0% found this document useful (0 votes)
22 views13 pages

8 Bitwise

Bitwise structures allow accessing and manipulating individual bits within an integer variable. While the smallest data type in C is a char (8 bits), sometimes only a single bit is needed, such as to represent the status (on/off) of multiple lights using a single variable. Bitwise operators like & (AND), | (OR), and ^ (XOR) allow setting, clearing, and checking individual bits by masking all but the desired bit. Shifting operators like << and >> are also used to position a single-bit mask over the desired bit. This approach uses memory more efficiently than storing each status in a separate variable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

8 Bitwise

Bitwise structures allow accessing and manipulating individual bits within an integer variable. While the smallest data type in C is a char (8 bits), sometimes only a single bit is needed, such as to represent the status (on/off) of multiple lights using a single variable. Bitwise operators like & (AND), | (OR), and ^ (XOR) allow setting, clearing, and checking individual bits by masking all but the desired bit. Shifting operators like << and >> are also used to position a single-bit mask over the desired bit. This approach uses memory more efficiently than storing each status in a separate variable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Programming Language

Bitwise Structures
What is Bitwise Structure?
• The smallest type is of 8 bits (char).
• Sometimes we need only a single bit.
• For instance, storing the status of the lights in 8 rooms:
▫ We need to define an array of at least 8 chars.
If the light of room 3 is turned on the value of
the third char is 1, otherwise 0.
▫ Total array of 64 bits.
What is Bitwise Structure?
• It is better to define only 8 bits since a bit can also store the values 0
or 1.
• But the problem is that there is no C type which is
1 bit long (char is the longer with 1 byte).
• Solution: define a char (8 bits) but refer to each bit separately.
• Bitwise operators, introduced by the C language, provide one of its
more powerful tools for using and manipulating memory. They give
the language the real power of a “low-level language”.
What is Bitwise Structure?
• Accessing bits directly is fast and efficient, especially if you are
writing a real-time application.
• A single bit cannot be accessed directly,
since it has no address of its own.
• The language introduces the bitwise operators, which help in
manipulating a
single bit of a byte.
• bitwise operators may be used on integral types only (unsigned
types are preferable).
What is Bitwise Structure?
• Accessing bits directly is fast and efficient, especially if you are
writing a real-time application.
• A single bit cannot be accessed directly,
since it has no address of its own.
• The language introduces the bitwise operators, which help in
manipulating a
single bit of a byte.
• bitwise operators may be used on integral types only (unsigned
types are preferable).
Bitwise Operators

& bitwise AND


| bitwise OR
^ bitwise XOR
~ 1’s compliment
<< Shift left
>> Shift right

All these operators can be suffixed with =


For instance a &= b; is the same as a = a & b;
Bitwise Operators – truth table
a b a&b a|b a^b ~a

0 0 0 0 0 1

0 1 0 1 1 1

1 0 0 1 1 0

1 1 1 1 0 0
Bitwise Operators - Examples
11010011 11010011 11010011
& | ^
10001100 10001100 10001100
------------ ------------ ------------
10000000 11011111 01011111

~11010011 11010011>>3 11010011<<3


------------ ------------ ------------
00101100 00011010 10011000
Setting Bits
• How can we set a bit on or off?
• Manipulations on bits are enabled by mask and bitwise operators.
• Bitwise OR of anything with 1 results in 1.
• Bitwise AND of anything with 0 results in 0.
Setting Bits
• For instance, how can we turn on the light in room #3?

lights: 00000000

char lights = 0x0; mask: 00000001


char mask = 0x1;
mask <<= 2;
lights |= mask; mask: 00000100

lights: 00000100
Setting Bits
• For instance, how can we turn off the light in room #3?

lights: 00100111

char lights = 0x27; mask: 11111011


char mask = 0xfb;
lights &= mask;
lights: 00100011
Getting Bits
• How can we know if a bit is on or off?
• Manipulations on bits are enabled by mask and bitwise operators.
• Bitwise AND of anything with 1 results in the same value.
Getting Bits
• For instance, how can we check if the light in room #3 is turned on or
off?

lights: 00100111
char lights = 0x27;
char mask = 0x1;
mask <<= 2; mask: 00000001
if(lights & mask)
mask: 00000100
puts(“turned on”);
else
puts(“turned off”); lights & mask: 00000100

13

You might also like