• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1375
  • Last Modified:

bitwise AND operation in XSL

Hi,

Can I test something like

<xsl:if test="(@access & 1) = 1">
</xsl:if>

Does xsl support bitwise operation like |, &, ^ etc.

Thanks,
Biswaranjan
0
biswaranjan_rath
Asked:
biswaranjan_rath
1 Solution
 
Geert BormansCommented:
Hi biswaranjan_rath,

there are logical operators such as "and", "or" and "not"
what is the content of @access and what do you try to achieve?

Cheers!
0
 
jkmyoungCommented:
You might be able to use:
<xsl:if test="@access mod 2 = 1">
</xsl:if>

Depends on what you need it for, as Gertone says.
0
 
biswaranjan_rathAuthor Commented:
@access has some signed decimal value. I wanted to do bitwise operation on that like &, |, ^ or <<, >> etc.

Can I be able to do this?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rdcproCommented:
I have a little article on doing bitwise comparisons:

http://rdcpro.com/Members/rdcpro/snippets/bitwise/

This is only marginally of help to you, because your numbers start out as base 10.  However, you could write a recursive template that would convert your base 10 value into a base 2 value, then do your bitwise comparison.

Regards,
Mike Sharp
0
 
biswaranjan_rathAuthor Commented:
i didn't able to understand anything from this link.
0
 
rdcproCommented:
Here's a complete example.  With this XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="decimal2binary2.xslt"?>
<root>
      <Decimal>6</Decimal>
      <Decimal>5</Decimal>
</root>

try this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="/root">
      
            <h3>  Bitwise Comparison of two decimal (base 10) numbers</h3>
            <p>
                  bitwise compare:
                  <br/>
                  <xsl:value-of select="Decimal[1]"/>
                  <br/>to<br/>
                  <xsl:value-of select="Decimal[2]"/>
            </p>  
            
            <p>First, convert to bitmask</p>      
            
            <xsl:variable name="vDecimal1">
                  <xsl:call-template name="tDecimal2Binary">
                        <xsl:with-param name="pDecimal" select="number(Decimal[1])"/>
                  </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="vDecimal2">
                  <xsl:call-template name="tDecimal2Binary">
                        <xsl:with-param name="pDecimal" select="number(Decimal[2])"/>
                  </xsl:call-template>
            </xsl:variable>
            
            <xsl:value-of select="Decimal[1]"/>
            <xsl:text> is </xsl:text>
            <xsl:value-of select="$vDecimal1"/>
            <xsl:text> in base 2 </xsl:text>
            <br/>
            <xsl:text>and</xsl:text>
            <br/>      
            <xsl:value-of select="Decimal[2]"/>
            <xsl:text> is </xsl:text>
            <xsl:value-of select="$vDecimal2"/>
            <xsl:text> in base 2 </xsl:text>
            
            <p>
                  <xsl:text>bitwise compare:   </xsl:text>      
                  <xsl:call-template name="tBitCompare">
                        <xsl:with-param name="pString1" select="$vDecimal1"/>
                        <xsl:with-param name="pString2" select="$vDecimal2"/>
                  </xsl:call-template>
            </p>
            <p>
                  <xsl:text>exclusive OR:   </xsl:text>      
                  <xsl:call-template name="tBitXOR">
                        <xsl:with-param name="pString1" select="$vDecimal1"/>
                        <xsl:with-param name="pString2" select="$vDecimal2"/>
                  </xsl:call-template>
            </p>
            <p>
                  <xsl:text>inclusive OR:   </xsl:text>      
                  <xsl:call-template name="tBitInOR">
                        <xsl:with-param name="pString1" select="$vDecimal1"/>
                        <xsl:with-param name="pString2" select="$vDecimal2"/>
                  </xsl:call-template>
            </p>

      </xsl:template>
      
      
      
      <xsl:template name="tDecimal2Binary">
            <xsl:param name="pDecimal"/>
            <xsl:if test="$pDecimal &gt; 0">
                  <!-- If condtion goes first to make sure the output is in reverse order -->
                  <xsl:call-template name="tDecimal2Binary">
                        <xsl:with-param name="pDecimal" select="floor($pDecimal div 2)"/>
                  </xsl:call-template>
                  <xsl:value-of select="$pDecimal mod 2"/>
            </xsl:if>
      </xsl:template>

      <xsl:template name="tBitCompare">
            <xsl:param name="pString1"/>
            <xsl:param name="pString2"/>
            <xsl:choose>
                  <xsl:when test="substring($pString1, 1, 1) = 1 and substring($pString2, 1, 1) = 1">true</xsl:when>
                  <xsl:when test="string-length($pString1) &gt; 1">
                        <xsl:call-template name="tBitCompare">
                              <xsl:with-param name="pString1" select="substring($pString1, 2)"/>
                              <xsl:with-param name="pString2" select="substring($pString2, 2)"/>
                        </xsl:call-template>
                  </xsl:when>
                  <xsl:otherwise>false</xsl:otherwise>
            </xsl:choose>
      </xsl:template>

      <!-- Exclusive OR -->
      <xsl:template name="tBitXOR">
            <xsl:param name="pString1"/>
            <xsl:param name="pString2"/>
            <xsl:choose>
                  <xsl:when test="substring($pString1, 1, 1) = 1 and substring($pString2, 1, 1) = 1">
                        <xsl:text>0</xsl:text>
                  </xsl:when>
                  <xsl:when test="substring($pString1, 1, 1) = 1 or substring($pString2, 1, 1) = 1">
                        <xsl:text>1</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
            <xsl:if test="string-length($pString1) &gt; 1">
                  <xsl:call-template name="tBitXOR">
                        <xsl:with-param name="pString1" select="substring($pString1, 2)"/>
                        <xsl:with-param name="pString2" select="substring($pString2, 2)"/>
                  </xsl:call-template>
            </xsl:if>
      </xsl:template>

      <!-- Inclusive OR -->
      <xsl:template name="tBitInOR">
            <xsl:param name="pString1"/>
            <xsl:param name="pString2"/>
            <xsl:choose>
                  <xsl:when test="substring($pString1, 1, 1) = 1 or substring($pString2, 1, 1) = 1">
                        <xsl:text>1</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
            <xsl:if test="string-length($pString1) &gt; 1">
                  <xsl:call-template name="tBitXOR">
                        <xsl:with-param name="pString1" select="substring($pString1, 2)"/>
                        <xsl:with-param name="pString2" select="substring($pString2, 2)"/>
                  </xsl:call-template>
            </xsl:if>
      </xsl:template>

</xsl:stylesheet>


which produces this output:

Bitwise Comparison of two decimal (base 10) numbers

bitwise compare:
6
to
5

First, convert to bitmask

6 is 110 in base 2
and
5 is 101 in base 2

bitwise compare: true

exclusive OR: 011

inclusive OR: 111


Regards,
Mike Sharp
0
 
biswaranjan_rathAuthor Commented:
Thanks Mike, this is working fine.
But finally I use javascript to avoid complications and heavy code size.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now