Copyright @ 2009
Ananda Gunawardena
Lecture 17
Bit Operations
In this lecture
•
Background
•
Left Shifting
•
Negative Numbers, one’s complement and two’s
complement
•
Right Shifting
•
Bit Operators
•
Masking the Bits
•
Getting the Bits
•
Setting the Bits
•
Binary Files
•
Bit fields
•
More Exercises
C is a powerful language and allows programmer many
operations for bit manipulation. Data can be accessed at
the bit level to make operations and storage more
efficient. As you will see, bit operations can be used to
do many things including setting flags, encrypting and
decrypting images as we will implement in one of the lab
assignments. Bit operations can also be used to efficiently
pack data into a more compressed form. For example, an
entire array of 16 boolean values can be represented by
just 2 bytes of data or an IP address such as 192.168.1.15
can be packed into 32 bits of storage. This can come very
handy in cases where data needs to be transmitted through a
limited bandwidth network or in cases where we simply need
to store data more efficiently for better memory management
in the application. For example, when programming mobile
devices with limited memory, you may want to work at the
bit level to make things more efficient and save memory.
Also understanding bit operations can be useful in writing
device drivers and many other low level applications. First
we will discuss the bit shift operations.
Left Shifting
Think of the following possibility. Suppose we want to
multiply an unsigned integer by 2. We can simply shift all
bits to the left by one position (assuming no overflow).
For example, if a 32bit unsigned integer 73 is given as
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Copyright @ 2009
Ananda Gunawardena
00000000 00000000 00000000 01001001
Then shifting the bits by 1 position to the left gives us
the bit pattern for unsigned 146
00000000 00000000 00000000 10010010
So we can write
unsigned int x = 73;
x = x<<1;
or simply
x <<= 1;
The operator << is used to shift the bit patterns of a
variable. For example, x << n shifts the bit patterns of x
by n positions resulting in the number x*2
n
assuming there
is no overflow.
It should be noted that as we left shift, the missing bits
on the right are filled by 0’s.
Negative Numbers, One’s Complement and Two’s
Complement
Signed data is generally represented in the computer in
their two’s complement. Two’s complement of a number is
obtained by adding 1 to its one’s complement. So how do we
find one’s complement of a number? Here is the definition
One’s complement of x is given by
~x
. Obtain the one’s
complement of a number by negating each of its binary bits.
For example one’s complement of 30 is (represented as a 16
bit short int)
30 = 16 + 8 + 4 + 2 = 00000001 11100000
barb4left
binary 30
~30 = 11111110 00011111
barb4left
its one’s complement
The two’s complement of the number is obtained by adding 1
to its one’s complement.
That is, the two’s complement of
30 is obtained as follows.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 bajkzek
 Binary numeral system, File system permissions, Bitwise operation, Ananda Gunawardena

Click to edit the document details