Professional Documents
Culture Documents
Description
AND
OR
XOR
shift bits right n places
shift bits left n places
complements (invert all bits)
Example
y = y & 0xF7;
y = y | 0x08;
y = y >> 4;
y = y << 2;
y = ~y;
Only use bit operators with integer data types - Not real data types.
3-1
OR
B Output
AND
B Output
XOR
B Output
0
0
1
0
1
0
0
1
1
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
1
1
0
3-2
Shift Operations
3-3
or
or
var &= ~0x20;
or
var &= ~(1UL << 5);
3-5
var
var
var
var
var
var
var
|=
&=
|=
&=
|=
&=
|=
var &= ~((3UL << 2*2) | (3UL << 2*3) | (3UL << 2*6) | (3UL << 2*7));
var |= ((1UL << 2*2) | (1UL << 2*3) | (1UL << 2*6) | (1UL << 2*7));
3-6
set bit 11
clear bit 13
set bits 6 and 10
clear bits 13 and 14
set bits 4,5,6, and 7
set bit 2 and clear bit 3
3-7
Input Masking
Used to test the value of a single bit.
Use AND (&) and a mask to isolate the selected bit
value & mask
Used in input Polling to test bit status
set bit(s) to be tested to '1', all other bits to '0'
Example: Testing bit 3
3-8
3-9
An Exercise
A32 bit register(REG) is interfaced to the following peripherals:A motor connected to bit 7 (0 is off, 1 is on)
An led connected to bit 0 (0 is off, 1 is on)
Two switches S1 and S2 connected to bits 1 and 2 respectively that
generate either logic 0 or 1 inputs.
Assume inputs start at logic 0 and all output are initialised to 0.