Documente Academic
Documente Profesional
Documente Cultură
0110 = 6h
OR 1010 = Ah
---EQUALS 1110 = Eh
In C language:
int foo = 0x6 & 0xA;
2h */
int foo = 0x6 | 0xA;
Eh */
0110
1010
0000
1111
equals
equals
equals
equals
1001
0101
1111
0000
-or-or-or-or-
~6h
~Ah
~0h
~Fh
equals
equals
equals
equals
9h
5h
Fh
0h
You will recoginze the above values from the 4-bit "hex
bit flip" table.
The NOT operator flips every bit.
Note that the word length (number of bits) is critical
when performing
a NOT operation, since *every bit* in the word flips,
even the bits you
don't see:
In C language:
char x = ~0;
/* result
short x = ~0;
/* result
int
x = ~0;
/* result
32-bit int */
int
x = ~0;
/* result
0xFFFFFFFFFFFFFFFF for 64-bit */
is 0xFF */
is 0xFFFF */
is 0xFFFFFFFF for
char x = ~0xF;
/* result
short x = ~0xF;
/* result
int
x = ~0xF;
/* result
32-bit int */
int
x = ~0xF;
/* result
0xFFFFFFFFFFFFFFF0 for 64-bit */
is 0xF0 */
is 0xFFF0 */
is 0xFFFFFFF0 for
int
x = ~0x1;
32-bit int */
int
x = ~0xFFFF;
32-bit int */
int
x = ~0xFFFF0000;
32-bit int */
is
is
Note how the bits in the mask "copy" the bits above.
The zeroes in the mask don't let any bits "flow down"
from above.
*** EXAMPLE 2: A mask to keep just the top nybble (4
bits) of a byte.
Create a mask that lets only the top 4 bits flow
through:
10101010 = AAh
AND 11110000 = F0h
-------EQUALS 10100000 = A0h
Note how the bits in the mask "copy" the bits above.
The zeroes in the mask don't let any bits "flow down"
from above.
*** EXAMPLE 3: ECOA2e "bit masking" example in text on p.
122:
Is the "4" bit set in a word? To find out, bitwise
AND the word with 04h.
If the resulting value is not zero, the "4" bit must
have been set.
3FC00000h
floating-point value
AND 80000000h
-------EQUALS 00000000h
positive)
Note how the bit in the mask "copies" the bit above.
The zeroes in the mask don't let any bits "flow down"
from above.
In C++ language (requires a cast to allow bitwise
operation on float):
float x = -1.5;
float y = 1.5;
if ( (unsigned)x & 0x80000000 ) cout << "x is
negative\n";
if ( (unsigned)y & 0x80000000 ) cout << "y is
negative\n";
Only "x is negative" prints.
*** EXAMPLE 5: Select just the CTRL (control) bits of an
ASCII character.
ASCII "control" characters are the 32 values from
0-31 decimal (00
to 1F hex). It takes 5 bits to hold these characters
(2**5 = 32).
Control characters are named by the ASCII letters
with which they are
associated. A CTRL-A is just the lower 5 bits of the
ASCII letter
'A' (or 'a'). A bit mask that selects just the lower
5 bits in an
8-bit byte is: 00011111 = 1Fh
Create a mask that lets only the bottom 5 bits flow
through:
01000001 = 41h = ASCII upper-case letter 'A'
AND 00011111 = 1Fh <-- this is the 5-bit mask
-------EQUALS 00000001 = 01h = CTRL-A
Note how the bits in the mask "copy" the bits above.
The zeroes in the mask don't let any bits "flow down"
from above.
This mask works if the letter is lower- or uppercase:
01100001 = 61h = ASCII lower-case letter 'a'
AND 00011111 = 1Fh <-- this is the 5-bit mask
-------EQUALS 00000001 = 01h = CTRL-A
Note how the bits in the mask "copy" the bits above.
The zeroes in the mask don't let any bits "flow down"
from above.
In C language:
char a = 'a';
char ctrla = a & 0x1F;
5 bits */
ORing in bits - turning on bits
------------------------------The opposite of masking (turning off bits) is "ORing in"
bits, where
we use the bitwise Boolean OR to "turn on" one or more
bits in a word.
We select a value that, when ORed with some other value,
"turns on"
selected bits and leaves the other bits unchanged.
*** EXAMPLE 6:
= 'A';
= uppera | 0x20;
/* bitwise OR with
= 'B' | 0x20;
/* bitwise OR with
= 'Z' | 0x20;
/* bitwise OR with
The same is not true if the letter is *already* lowercase, e.g. for
'a' = 61h, adding 20h gives 81h which isn't even an ASCII
character
(since there are only 128 ASCII characters from 00h to
7Fh):
'a' + 20h --> 81h
ASCII 81h ***
61h + 20h --> 81h
bitwise NOT
to 20h to do this:
Take 00100000 = 20h -> apply NOT (bit flip) ->
11011111 = DFh.
Let's try the mask on a sample ASCII lower-case
letter 'a'.
(ASCII 'a' has value 61h = 01100001.) Any letter
would work.
01100001 = 61h = ASCII lower-case letter 'a'
AND 11011111 = DFh <-- this is the bitwise NOT
(bit-flip) of 20h
-------EQUALS 01000001 = 41h = ASCII upper-case letter 'A'
In C language:
*/
char
with ~20h =
char
with ~20h =
The same is not true if the letter is *already* uppercase, e.g. for
'A' = 41h, subtracting 20h gives 21h which is '!':
'A' - 20h --> '!'
41h - 20h --> 21h
/* result is 0xF7 */
/* result is 0xFFF7 */
/* result is 0xFFFFFFF7 for 32-bit
int
x = ~0x8;
for 64-bit int */
/* result is 0xFFFFFFFFFFFFFFF7