Why does SELECT 2^3 return 1 in SQL Server? [duplicate] Why does SELECT 2^3 return 1 in SQL Server? [duplicate] sql sql

# Why does SELECT 2^3 return 1 in SQL Server? [duplicate]

Because ^ is XOR operator.

Truth table for XOR

``-------|^|1|0|-------|1|0|1|-------|0|1|0|-------``

In another words in result we have have one only when two bits are different.

``1 ^ 1 = 01 ^ 0 = 10 ^ 1 = 10 ^ 0 = 0``

For You case it its

``binary - decimal00010  - 200011  - 3---------- ^00001  - 1``

Usage

For example for mask (bitwise operations) handling or cryptography.

``a b a^b-------0 0  00 1  11 0  11 1  02   0b103   0b11--------2^3 0b01 = 1``

The real practical use is to toggle bits when used as flags.

it's the Bitwise Exclusive OR.

The truth tables for OR and XOR (exclusive OR) are different. OR means 'If either of these inputs are true, the output is true'. XOR means 'If one or the other, but not both of the inputs are true, the output is true'

OR truth table:false OR false = falsetrue OR false = truefalse OR true = truetrue OR true = true

XOR truth table:false XOR false = falsetrue XOR false = truefalse XOR true = truetrue XOR true = false

So what the query is doing is converting each input into binary, then doing an XOR on each bit.2 ^ 3:

``    00000010 (2) XOR    00000011 (3)=     00000001``