Bit Masks
From PREX Wiki
Jump to navigationJump to search
PREX Main<< HOW TOs
Bit-wise operations
- A 16 bit hexadecimal integer looks like
- n = (digits) 0x4th 3rd 2nd 1st = n_16 n_15 n_14 n_13 | n_12 n_11 n_10 n_9 | n_8 n_7 n_6 n_5 | n_4 n_3 n_2 n_1
- And the number 0x0177 is arrived at by
- n = 0x0177 = 0000|0001|0111|0111
Hexadecimal numbering system
Hex Digit |
Decimal |
binary hex components |
binary |
binary hex components powers |
Decimal
|
0 |
0 |
0000 |
0 |
0^3 + 0^2 + 0^1 + 0^0 |
0
|
1 |
1 |
0001 |
1 |
0^3 + 0^2 + 0^1 + 2^0 |
1
|
2 |
2 |
0010 |
10 |
0^3 + 0^2 + 2^1 + 0^0 |
2
|
3 |
3 |
0011 |
11 |
0^3 + 0^2 + 2^1 + 2^0 |
3
|
4 |
4 |
0100 |
100 |
0^3 + 2^2 + 0^1 + 0^0 |
4
|
5 |
5 |
0101 |
101 |
0^3 + 2^2 + 0^1 + 2^0 |
5
|
6 |
6 |
0110 |
110 |
0^3 + 2^2 + 2^1 + 0^0 |
6
|
7 |
7 |
0111 |
111 |
0^3 + 2^2 + 2^1 + 2^0 |
7
|
8 |
8 |
1000 |
1000 |
2^3 + 0^2 + 0^1 + 0^0 |
8
|
9 |
9 |
1001 |
1001 |
2^3 + 0^2 + 0^1 + 2^0 |
9
|
a |
10 |
1010 |
1010 |
2^3 + 0^2 + 2^1 + 0^0 |
10
|
b |
11 |
1011 |
1011 |
2^3 + 0^2 + 2^1 + 2^0 |
11
|
c |
12 |
1100 |
1100 |
2^3 + 2^2 + 0^1 + 0^0 |
12
|
d |
13 |
1101 |
1101 |
2^3 + 2^2 + 0^1 + 2^0 |
13
|
e |
14 |
1110 |
1110 |
2^3 + 2^2 + 2^1 + 0^0 |
14
|
f |
15 |
1111 |
1111 |
2^3 + 2^2 + 2^1 + 2^0 |
15
|
Bitwise Operators
- Logical And (&) -> a = b & c
- Takes the bitwise (0's and 1's) representation of b and c and the result of each bit's & is "a"
- Logical Inclusive Or (|) -> a = b | c
- Takes the bitwise representation, and if either is 1 then a's corresponding bit is 1
- Logical Exclusive Or (^) -> a = b^c
- Takes the bitwise representation, and if just one of b or c's bits is 1 then a's corresponding bit is too, but if both are == then the result is 0
- Logical Not (~) -> a = ~b
- Takes each bit and returns its complement
- Bit shift (>> or <<) -> a = b>>N
- Takes the bit contents of b and shifts them N places to the right to produce a
- Examples
- Grab the 3rd digit of a hexadecimal integer and make that a new variable -> mask the other digits and shift the result to the right by 2
- Mask off the top 8 bits (8th and 7th digit) of a 32 bit integer
- Mask off all but the last bit
- Mask off just the last bit
Japan Error Code Bit Masks