|
Category: ASE: Operators
ASE Bit operators, Bit operators,
A bit
operator
is part of a database query. Using bit operators it is possible to directly display the binary representation of numbers.
Bit Operators and Examples
-
& (ampersand) - AND
returns 1 (0101 & 0011 = 0001)
-
| (vertical bar, pipe) - OR
returns 7 (0101 | 0010 = 0111)
-
(circumflex) - EXOR
returns 6 (0101 0011 = 0110)
-
~ (tilde) - NOT
returns -2 (~0001 = 1110)
Examples and rules for bit manipulation
AND - & (ampersand)
Using the bit operator &, one can test if a given number is odd or even.
It is sufficient to test if bit 0 (i.e. the first bit) is set, that means it has a value of "1" (odd) or not, that means it is set to "0" (even).
The above example returns 7.
| Rules for bitwise AND conjunction (&)
|
| BitA |
BitB |
BitA & BitB |
| 0 |
0 |
0 |
| 0 |
1 |
0 |
| 1 |
0 |
0 |
| 1 |
1 |
1 |
OR - | (vertical bar, pipe)
With the help of bitwise OR additional bits can be systematically set or unset.
select 1|126
go
-----
127
The above example returns 127.
| Rules for bitwise OR conjunction (|)
|
| BitA |
BitB |
BitA | BitB |
| 0 |
0 |
0 |
| 0 |
1 |
1 |
| 1 |
0 |
1 |
| 1 |
1 |
1 |
EXOR - (circumflex)
The exclusive OR operator will only then return 1, if both bits are set different. This is useful to switch certain bits. All bits set will be unset and vice versa.
The above example returns 35.
| Rules for bitwise EXOR conjunction ()
|
| BitA |
BitB |
BitA BitB |
| 0 |
0 |
0 |
| 0 |
1 |
1 |
| 1 |
0 |
1 |
| 1 |
1 |
0 |
NOT - ~ (tilde)
The NOT operator (~) inverts every single bit of a given number. With signed datatypes, this will result in a negation with subsequent subtraction of 1.
The above example returns -21.
| Regeln der bitweisen NOT-Verknüpfung (~)
|
| BitA |
~BitA |
| 0 |
1 |
| 1 |
0 |
See also:
Numeric Operators, Pattern Matching Operators.
|