[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Bitwise Operations:More efficient solution?


Subject: Re: [xsl] Bitwise Operations:More efficient solution?
From: Chris Maloney <voldrani@xxxxxxxxx>
Date: Tue, 15 Feb 2011 17:58:44 -0500

I can see one tweak -- if the arguments are really octet values
("bytes") then ($byte<n> mod 256) is always 0.

I'd be tempted to do something with tail-recursion, but your
implementation is probably much more efficient.

On Tue, Feb 15, 2011 at 5:41 PM, Eliot Kimber <ekimber@xxxxxxxxxxxx> wrote:
> I have implemented UTF-8 URI decoding in XSLT 2. It required that I
> implement left-shift and bitwise AND operations. Left-shift is easy but
> bitwise AND is a little more involved. My solution is below, but I'm
> wondering if there's a more elegant and/or efficient way to do this?
>
> My approach was based on info from Ken Holman's site that shows how to
> calculate each bit of the byte. I do that and then sum the result of
> multiplying the AND of each bit pair times the appropriate power of two.
>
> B <xsl:function name="relpath:bitwiseAnd" as="xs:integer">
> B  B <xsl:param name="byte1" as="xs:integer"/>
> B  B <xsl:param name="byte2" as="xs:integer"/>
>
> B  B <xsl:variable name="byte1Bit7" select="if (($byte1 mod 256) - ($byte1
> mod 128) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit6" select="if (($byte1 mod 128) - ($byte1
> mod 64) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit5" select="if (($byte1 mod 64) B - ($byte1
> mod 32) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit4" select="if (($byte1 mod 32) B - ($byte1
> mod 16) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit3" select="if (($byte1 mod 16) B - ($byte1
> mod 8) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit2" select="if (($byte1 mod 8) B  - ($byte1
> mod 4) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit1" select="if (($byte1 mod 4) B  - ($byte1
> mod 2) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte1Bit0" select="if (($byte1 mod 2) B > 0) then
1
> else 0" as="xs:integer"/>
>
> B  B <xsl:variable name="byte2Bit7" select="if (($byte2 mod 256) - ($byte2
> mod 128) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit6" select="if (($byte2 mod 128) - ($byte2
> mod 64) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit5" select="if (($byte2 mod 64) B - ($byte2
> mod 32) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit4" select="if (($byte2 mod 32) B - ($byte2
> mod 16) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit3" select="if (($byte2 mod 16) B - ($byte2
> mod 8) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit2" select="if (($byte2 mod 8) B  - ($byte2
> mod 4) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit1" select="if (($byte2 mod 4) B  - ($byte2
> mod 2) > 0) then 1 else 0" as="xs:integer"/>
> B  B <xsl:variable name="byte2Bit0" select="if (($byte2 mod 2) B > 0) then
1
> else 0" as="xs:integer"/>
>
> B  B <xsl:variable name="result" as="xs:integer"
> B  B  B select="
> B  B  B (128 * $byte2Bit7 * $byte1Bit7) +
> B  B  B (64 * $byte2Bit6 * $byte1Bit6) +
> B  B  B (32 * $byte2Bit5 * $byte1Bit5) +
> B  B  B (16 * $byte2Bit4 * $byte1Bit4) +
> B  B  B (8* $byte2Bit3 * $byte1Bit3) +
> B  B  B (4 * $byte2Bit2 * $byte1Bit2) +
> B  B  B (2 * $byte2Bit1 * $byte1Bit1) +
> B  B  B (1 * $byte2Bit0 * $byte1Bit0)
> B  B  B "
> B  B />
> B  B <xsl:sequence select="$result"/>
> B </xsl:function>
>
> Cheers,
>
> E.
> --
> Eliot Kimber
> Senior Solutions Architect
> "Bringing Strategy, Content, and Technology Together"
> Main: 512.554.9368
> www.reallysi.com
> www.rsuitecms.com


Current Thread
Keywords