A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  special characters  sybase-tech-blog


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
        select 5 & 3
    go
    -----
    1
      returns 1 (0101 & 0011 = 0001)

  • | (vertical bar, pipe) - OR
        select 5 | 2
    go
    -----
    7
      returns 7 (0101 | 0010 = 0111)

  • ˆ (circumflex) - EXOR
        select 5 ˆ 3
    go
    -----
    6
      returns 6 (0101 ˆ 0011 = 0110)

  • ~ (tilde) - NOT
        select ~1
    go
    -----
    -2
      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).

    select 55 & 7
go
-----
7

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.

    select 20ˆ55
go
-----
35

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.

    select ~20
go
-----
-21

The above example returns -21.

Regeln der bitweisen NOT-Verknüpfung (~)
BitA ~BitA
0 1
1 0

See also:

Numeric Operators,
Pattern Matching Operators.