Logical operators  Shift operators

Chapter 1: Overview

Bitwise operators

Bitwise operators compare each corresponding bit in the expressions and produce a result based upon that comparison. Bitwise operators apply only to integer type expressions.

In the examples below, the bitwise value of “a” is 1010 0110 and the bitwise value of “b” is 0011 1011.

Table 1-6: Bitwise operators

Operator

Description

Example

&

Bitwise AND.

Sets the bit if both bits are set.

c = a & b;

The bitwise value of “c” is 0010 0010.

&=

Bitwise AND and assign.

This is the same as a = a & b;.

a &= b;

The new bitwise value of “a” is 0010 0010.

|

Bitwise OR.

Sets the bit if either bit is set.

c = a | b;

The bitwise value of “c” is 1011 1111.

|=

Bitwise OR and assign.

This is the same as a = a | b;.

a |= b;

The new bitwise value of “a” is 1011 1111.

^

Bitwise exclusive OR.

Sets the bit when only one of the bits is set.

c = a ^ b;

The bitwise value of “c” is 1001 1101.

^=

Bitwise exclusive OR and assign.

This is the same as a = a ^ b;.

a ^= b;

The new bitwise value of “a” is 1001 1101.

~

1’s compliment.

Reverses the setting of each bit.

c = ~b;

The bitwise value of “c” is 1100 0100.





Copyright © 2005. Sybase Inc. All rights reserved. Shift operators

View this book as PDF