[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Bitwise Operations:More efficient solution?

Subject: Bitwise Operations:More efficient solution?
From: Eliot Kimber <ekimber@xxxxxxxxxxxx>
Date: Tue, 15 Feb 2011 16:41:02 -0600
 Bitwise Operations:More efficient solution?
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.

  <xsl:function name="relpath:bitwiseAnd" as="xs:integer">
    <xsl:param name="byte1" as="xs:integer"/>
    <xsl:param name="byte2" as="xs:integer"/>

    <xsl:variable name="byte1Bit7" select="if (($byte1 mod 256) - ($byte1
mod 128) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit6" select="if (($byte1 mod 128) - ($byte1
mod 64) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit5" select="if (($byte1 mod 64)  - ($byte1
mod 32) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit4" select="if (($byte1 mod 32)  - ($byte1
mod 16) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit3" select="if (($byte1 mod 16)  - ($byte1
mod 8) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit2" select="if (($byte1 mod 8)   - ($byte1
mod 4) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit1" select="if (($byte1 mod 4)   - ($byte1
mod 2) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte1Bit0" select="if (($byte1 mod 2)  > 0) then 1
else 0" as="xs:integer"/>

    <xsl:variable name="byte2Bit7" select="if (($byte2 mod 256) - ($byte2
mod 128) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit6" select="if (($byte2 mod 128) - ($byte2
mod 64) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit5" select="if (($byte2 mod 64)  - ($byte2
mod 32) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit4" select="if (($byte2 mod 32)  - ($byte2
mod 16) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit3" select="if (($byte2 mod 16)  - ($byte2
mod 8) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit2" select="if (($byte2 mod 8)   - ($byte2
mod 4) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit1" select="if (($byte2 mod 4)   - ($byte2
mod 2) > 0) then 1 else 0" as="xs:integer"/>
    <xsl:variable name="byte2Bit0" select="if (($byte2 mod 2)  > 0) then 1
else 0" as="xs:integer"/>

    <xsl:variable name="result" as="xs:integer"
      select="
      (128 * $byte2Bit7 * $byte1Bit7) +
      (64 * $byte2Bit6 * $byte1Bit6) +
      (32 * $byte2Bit5 * $byte1Bit5) +
      (16 * $byte2Bit4 * $byte1Bit4) +
      (8* $byte2Bit3 * $byte1Bit3) +
      (4 * $byte2Bit2 * $byte1Bit2) +
      (2 * $byte2Bit1 * $byte1Bit1) +
      (1 * $byte2Bit0 * $byte1Bit0)
      "
    />
    <xsl:sequence select="$result"/>
  </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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.