Review:
How to
set the bit at
position k to 1
- The
SETBIT(x, k)
operation:
(sets the bit
at position
k in
x)
position k
|
V
Before SETBIT(x,k): x = 1010...?...1101
After SETBIT(x,k): x = 1010...1...1101
|
- Solution:
initially,
the bit at
position k has
the value
?
(the value can be
0 or 1)
x = 1010...?...1101
pattern = 0000...1...0000
---------------- OR
1010...1...1101
|
|
Review:
How to
set the bit at
position k to 1
- The
SETBIT(x, k)
operation:
(sets the bit
at position
k in
x)
position k
|
V
Before SETBIT(x,k): x = 1010...?...1101
After SETBIT(x,k): x = 1010...1...1101
|
- Solution:
construct the
pattern
000..00100..000
(it has a 1
at position k):
x = 1010...?...1101
pattern = 000..00100..000
---------------- OR
1010...1...1101
|
|
Review:
How to
set the bit at
position k to 1
DEMO:
demo/C/set1/bit-op2a.c
How to
clear the bit at
position k to 1
- The
CLRBIT(x, k)
operation:
(clears the bit
at position
k in
x)
position k
|
V
Before CLRBIT(x,k): x = 1010...?...1101
After CLRBIT(x,k): x = 1010...0...1101
|
- Solution:
initially,
the bit at
position k has
the value
?
(the value can be
0 or 1)
x = 1010...?...1101
pattern = 0000...1...0000
---------------- OR
1010...1...1101
|
|
How to
clear the bit at
position k to 1
- The
CLRBIT(x, k)
operation:
(clears the bit
at position
k in
x)
position k
|
V
Before CLRBIT(x,k): x = 1010...?...1101
After CLRBIT(x,k): x = 1010...0...1101
|
- Solution:
construct the
pattern
111..11011..111
(it has a 0
at position k):
x = 1010...?...1101
pattern = 111..11011..111
---------------- OR
1010...1...1101
|
|
How to
clear the bit at
position k to 1
How to
obtain
the pattern used in the
CLRBIT(x,k) operation
- The
pattern used to
clear a
bit at
position
k is:
position k
|
V
pattern = 111..11011..111
|
- It's
easier to
construct the
pattern
using
the pattern
000..00100..000:
111..11011..111 ≡ ~ 000..00100..000
^
|
the bitwise NOT operation (flip each bit) !
|
|
DEMO:
demo/C/set1/bit-op3.c
How to
flip the bit at
position k
- The
FLIPBIT(x, k)
operation:
(flips the bit
at position
k in
x)
position k
|
V
Before FLIPBIT(x,k): x = 1010...?...1101
After FLIPBIT(x,k): x = 1010...¿...1101
|
- Solution:
initially,
the bit at
position k has
the value
?
(the value can be
0 or 1)
x = 1010...?...1101
pattern = 0000...1...0000
---------------- OR
1010...1...1101
|
|
How to
flip the bit at
position k
- The
FLIPBIT(x, k)
operation:
(flips the bit
at position
k in
x)
position k
|
V
Before FLIPBIT(x,k): x = 1010...?...1101
After FLIPBIT(x,k): x = 1010...¿...1101
|
- Solution:
construct the
pattern
000..00100..000
(it has a 1
at position k):
x = 1010...?...1101
pattern = 000..00100..000
---------------- OR
1010...1...1101
|
|
How to
flip the bit at
position k
DEMO:
demo/C/set1/bit-op5.c
How to
test if the bit at
position k is
0 or
1
- The
ISSET(x, k)
operation:
(returns
true (!= 0)
if bit k in
x is
1, and
returns
false (== 0)
otherwise)
position k
| if ? == 0: ISSET(x,k) == 0
V if ? == 1: ISSET(x,k) != 0
x = 1010...?...1101
|
- Solution:
initially,
the bit at
position k has
the value
?
(the value can be
0 or 1)
x = 1010...?...1101
pattern = 0000...1...0000
---------------- OR
1010...1...1101
|
|
How to
test if the bit at
position k is
0 or
1
- The
ISSET(x, k)
operation:
(returns
true (!= 0)
if bit k in
x is
1, and
returns
false (== 0)
otherwise)
position k
| if ? == 0: ISSET(x,k) == 0
V if ? == 1: ISSET(x,k) != 0
x = 1010...?...1101
|
- Solution:
construct the
pattern
000..00100..000
(it has a 1
at position k):
x = 1010...?...1101
pattern = 000..00100..000
---------------- OR
1010...1...1101
|
|
How to
test if the bit at
position k is
0 or
1
DEMO:
demo/C/set1/bit-op4.c
❮
❯