Copyright © 1997-2009 Curt Hill
BitWise Operators
Packing Logicals with Other Bases as a Bonus
Copyright © 1997-2009 Curt Hill
Aside• Before discussing bitwise operators
a more fundamental item needs to be introduced or reviewed
• Binary is the base 2 number system
• It is how things are represented in computers
• If an item cannot be represented in base two it cannot be stored on a computer
Copyright © 1997-2009 Curt Hill
Binary• Base two number system• Each digit is 0 or 1 and multiplied
by a power of two• Hence a byte with just one bit
turned on is a power of two• All numbers on a computer are
represented in binary because of the hardware
Copyright © 1997-2009 Curt Hill
8 bit example:
1 1 1 1 1 1 1 1
1 x 20 = 11 x 21 = 21 x 22 = 41 x 23 = 81 x 24 = 161 x 25 = 321 x 26 = 641 x 27 = 128
Copyright © 1997-2009 Curt Hill
Other bases• Binary has some problems, the
largest of which is that it is very bulky
• Decimal from binary is not an easy conversion– It also obscures structure
• Most manufacturers use another power of two base to show things– Octal – base 8 = 23
– Hexadecimal – base 16 = 24
Copyright © 1997-2009 Curt Hill
Octal• Base 8 has 8 digits, 0 – 7 and
every place is raised to a power of 8
• Conversion to and from binary is way too easy– Partition into groups of 3 bits from
right to left– Convert the three bits to octal
• Preferred by certain manufacturers such as DEC and Intel
Copyright © 1997-2009 Curt Hill
Binary – Octal Conversion
Start with binary 0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1
Partition (right to left) in groups of 3
0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1
Write digits 0 6 7 1 3 5
How easy is that?
Conversion of octal to binary is the reverse
Copyright © 1997-2009 Curt Hill
Hexadecimal• Base 16 has 16 digits, 0 – 9, A-F and
every place is raised to a power of 16– A=10, B=11, C=12, D=13, E=14, F=15
• Conversion to and from binary is also easy– Partition into groups of 4 bits– Convert the four bits to hexadecimal
• Preferred by certain manufacturers such as IBM and Burroughs
Copyright © 1997-2009 Curt Hill
Binary – Hexadecimal Conversion
Start with binary 0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1
Partition (right to left) in groups of 4
0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1
Write digits 6 E 5 D
How easy is that?
Conversion of hexadecimal to binary is the reverse
Copyright © 1997-2009 Curt Hill
C Notation• Since the C family of languages is
occasionally well designed they all provide a notation for octal and hexadecimal constants
• An octal constant must start with a zero and have only octal digits:– 031, 077123
• A hexadecimal constant starts with a zero and then an x:– 0x012cf8
Copyright © 1997-2009 Curt Hill
Purpose• Bool values may contain one of
two values: – true or false
• Boolean variables take one byte each
• Each byte consists of 8 bits, each with a possible value of 0 or 1
• Bitwise operators allow us to use these bits like small arrays of bools
Copyright © 1997-2009 Curt Hill
Packing• An integer is normally 4 bytes or
32 bits• An array of 4 bools occupies 4
bytes• In same space 4 booleans or 32
bits?• Allocation is always by bytes so we
cannot allocate 25 bits
Copyright © 1997-2009 Curt Hill
Boolean operations• AND • OR• NOT• XOR
Copyright © 1997-2009 Curt Hill
What is needed?• Declare these clusters of booleans• Initialize• Apply the operators• Obtain the results
Copyright © 1997-2009 Curt Hill
Declaration• Any simple type may be used• The preference is for integral types• char gives 8 bits• short gives 16• int gives 32• double gives 64
Copyright © 1997-2009 Curt Hill
Operators• Bitwise operators are the same as
the bool operators except one symbol instead of two– && is boolean AND– & is bit AND– | is OR– ^ is XOR
Copyright © 1997-2009 Curt Hill
Example• int a, b, c;• c = a | b• This does 32 individual Ors in one
operation– Usually a very fast operation
• 10110111 ^ 00101101 gives 10011010
Copyright © 1997-2009 Curt Hill
What about NOT?• To accomplish NOT use XOR with
all ones for one operand• 11111111 ^
01100111 gives 10011000
• Negation exampleint c;…c = c ^ 0xFFFFFFFF
Copyright © 1997-2009 Curt Hill
Accessing results• Integers cannot be used as bools• There is no subscripting operation
for bits either• How are they accessed? • Using bit operations and constant
integers
Copyright © 1997-2009 Curt Hill
Setting Values• To set values
– Add the power of two constants– c = 64 + 8 + 4 + 1;
• To check the presence of a bit– AND it with constant and check for
zero– c & 8 != 0
• true 8 was present• false 8 was not present
Copyright © 1997-2009 Curt Hill
Shifting
• There are also shift operations• A left shift of one bit corresponds
to multiply by 2• A right shift of one bit corresponds
to divide by 2• Form is int << int
– Left int is one to be shifted– Right is number of bits to shift
Copyright © 1997-2009 Curt Hill
Example Shift• c = a << 2;
– Same as multiply by 4 if a and c are not signed
• b = c >> a;– Shift the value from c to the right by a
bits and put result in b
Copyright © 1997-2009 Curt Hill
In Theory and In Practice• The usual use of bits are for named
constants• An Event object contains an id
field, which has these flags among others– MOUSE_ENTER, MOUSE_DRAG,
MOUSE_EXIT, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE
– ACTION_EVENT– LIST_SELECT, LIST_DESELECT
Copyright © 1997-2009 Curt Hill
Sets• Boolean algebra and set theory are
orthogonal– Any set operation or proof may be
converted into a boolean operation or proof or vice versa
• Thus an array of booleans may act like a set of small integers
Copyright © 1997-2009 Curt Hill
Set Operations• The boolean AND is equivalent to
the set Intersection– A & B could be either intersection or
parallel And
• The boolean OR is the set Union• The membership function of a set
is merely determining if that integer is present