Bitwise Operator Structures
Bitwise Operator Structures
C programming Language
Chapter 12:
Bitwise Structures
Lecturer: Natalie Fridman
11010011
|
10001100
-----------11011111
11010011>>3
-----------00011010
Bitwise Operators
11010011
^
10001100
-----------01011111
&
|
^
~
<<
>>
11010011<<3
-----------10011000
bitwise AND
bitwise OR
bitwise XOR
1s compliment
Shift left
Shift right
Bitwise Operators
truth table
Setting Bits
a&b
a|b
a^b
~a
Getting Bits
Setting Bits
How
Manipulations
on bits are
enabled by mask and bitwise
operators.
Bitwise
mask: 00000001
mask: 00000100
lights: 00000100
11
Getting Bits
Setting Bits
lights: 00100111
lights: 00100111
char lights = 0x27;
char mask = 0xfb;
lights &= mask;
mask: 00000001
mask: 00000100
mask: 11111011
lights: 00100011
10
Bitwise - Example
void set_lights(unsigned char *lights)
{
int j, answer;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
answer=0;
printf(lights in room #%d?, j+1);
scanf(%d, &answer);
if(answer)
*lights |= mask;
}
}
Bitwise - Example
Suppose we have 8 rooms:
lights
0 0 0 0 0 0 0 1
mask
0 0 0 0 0 0 1 0
answer: 0 (no)
void main()
{
unsigned char lights = 0;
set_lights(&lights);
print_lights(lights);
}
*lights |= mask
0 0 0 0 0 0 0 1
15
Bitwise - Example
void set_lights(unsigned char *lights)
{
int j, answer;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
answer=0;
printf(lights in room #%d?, j+1);
scanf(%d, &answer);
if(answer)
*lights |= mask;
}
}
Bitwise - Example
void set_lights(unsigned char *lights)
{
int j, answer;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
answer=0;
printf(lights in room #%d?, j+1);
scanf(%d, &answer);
if(answer)
*lights |= mask;
}
}
lights
0 0 0 0 0 0 0 1
mask
0 0 0 0 0 1 0 0
answer: 1 (yes)
*lights |= mask
0 0 0 0 0 1 0 1
16
13
lights
0 0 0 0 0 0 0 0
mask
0 0 0 0 0 0 0 1
answer: 1 (yes)
*lights |= mask
0 0 0 0 0 0 0 1
14
Bitwise - Example
void print_lights(unsigned char lights)
{
int j;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
if(lights & mask)
printf(room #%d is lighted,j+1);
else
printf(room #%d is NOT lighted,j+1);
}
}
Bitwise - Example
void set_lights(unsigned char *lights)
{
int j, answer;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
answer=0;
printf(lights in room #%d?, j+1);
scanf(%d, &answer);
if(answer)
*lights |= mask;
}
}
lights
0 0 0 0 1 1 0 1
mask
0 0 0 0 0 0 1 0
lights
0 0 0 0 0 1 0 1
mask
0 0 0 0 1 0 0 0
answer: 1 (yes)
*lights |= mask
0 0 0 0 1 1 0 1
Bitwise - Example
void print_lights(unsigned char lights)
{
int j;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
if(lights & mask)
printf(room #%d is lighted, j+1);
else
printf(room #%d is NOT lighted, j+1);
}
}
Bitwise - Example
void print_lights(unsigned char lights)
{
int j;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
if(lights & mask)
printf(room #%d is lighted,j+1);
else
printf(room #%d is NOT lighted,j+1);
}
}
lights
0 0 0 0 1 1 0 1
mask
0 0 0 0 0 1 0 0
room #3 is lighted
20
17
lights
0 0 0 0 1 1 0 1
mask
0 0 0 0 0 0 0 1
room #1 is lighted
18
Bitwise - Example
void print_lights(unsigned char lights)
{
int j;
unsigned char mask;
for(j=0,mask=1; j<8; j++,mask<<=1)
{
if(lights & mask)
printf(room #%d is lit, j+1);
else
printf(room #%d is NOT lit, j+1);
}
}
lights
0 0 0 0 1 1 0 1
mask
0 0 0 0 1 0 0 0
room #4 is lighted
21