Examples of manipulating the Colour Attribute Byte
The bitwise AND Operator ( & )
[Back]
For our example, we need to determine the colour of the text and the background independently. How do we read just the first four bits? Easy, the AND bitwise operator.
The bitwise AND operator examines each bit and returns a comparison. If a bit from Number A is 1, AND the corresponding bit from Number B is 1, the result is 1.Lets assume we have a BLUE Background with LIGHTCYAN text, and the attribute byte is stored in txtcolor.attr as 27 decimal, or 0001 10111 binary.
AND to Test the Foreground
Decimal | Binary | Operation | ||||||||
27 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | TEST bit | |
16 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | AND ( & ) | |
11 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | Result |
we’d write that as….(16 & txtcolor.attr)…and the result would be decimal 11, LIGHTCYAN
And to Test the Background
Decimal | Binary | Operation | ||||||||
27 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | TEST bit | |
112 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | AND ( & ) | |
16 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | Result |
and then the shift (112 & txtcolor.attr)>>3 the result would be 1, or BLUE
The bit wise AND can also be used to test a bit as follows:
Suppose we want to test for the Intensity bit (bit 3)
if ((txtcolor.attr & 0x08) ==0x08) {…} /*test if bit 3 is =1 */
Decimal | Binary | Operation | ||||||||
27 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | TEST bit | |
8 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | AND ( & ) | |
8 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | Result |
we could also write this as:
if ((txtcolor.attr & 0x08)>>3) {…}
this would shift the answer to the bit 0 position, and the result would be 1 if true and 0 if false then. In our case, the if statement would test true.
The Bitwise Inclusive OR ( | )
The bitwise OR operator examines each bit and returns a comparison. If the bit from Number A is 1 OR the bit from Number B is 1, then the result is 1. Suppose our foreground is MAGENTA, and our background is GREEN , therefor our attribute byte is 37 decimal, and we want to make MAGENTA into LIGHTMAGENTA, and BLINKING so, we add the blinking bit 7 and the high intensity bit 3.
OR to Set a Bit
Decimal | Binary | Operation | ||||||||
37 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | SET bit | |
136 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | OR ( | ) | |
173 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | Result |
We’d write that as (136 | txtcolor.attr)
A nice way to add a bit to the attribute. ..isn’t it. But if the bit is already set as you want it…it doesn’t change
We could get our bits as we did in the AND example, using OR, and a little subtraction.
Examine this example:
Foreground colour- CYAN, decimal 3, binary 0 0 1 1
Background colour- BLUE, decimal 1, binary 0 0 0 1
OR to Test a Bit
Decimal | Binary | Operation | ||||||||
19 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | TEST bit | |
240 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | OR ( | ) | |
243 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | Result |
now, if we subtract 240 from the result, it would give us decimal 3, CYAN.
we’d write it like this (240 | txtcolor.attr)-240
INVERSE ( ~ ) and Exclusive OR [XOR] ( ^ )
The next operator, INVERSE, does exactly as the name suggests, it turns 1’s into 0’s, and vice versa.
Consider how we could use Inverse to clear a bit. Suppose we have LIGHTCYAN text on a BLUE background, and we want clear the Intensity bit, to make the text CYAN
INVERSE and AND to clear a Bit
Decimal | Binary | Operation | ||||||||
27 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | CLEAR bit | |
~8 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | AND ( & ) | |
19 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | Result |
And you’d write it as txtcolor.attr &= ~0x08
NOTE- txtcolor.attr should be of the unsigned char type (8 bits)
What if you wanted to note only if the bits are different?..You guessed it…the exclusive OR.
Exclusive OR results in 0 if the bits are the same (either both 0 or both 1) and results in 1 if they are different.
Exclusive OR
Decimal | Binary | Operation | ||||||||
162 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | XOR ( ^ ) | |
203 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | ||
105 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | Result |
now, check this out…you can toggle a bit (if its 1, make it 0 or if its 0, make it 1) with Exclusive OR.
Lets look at bit 3 in our example value 162..
Toggling with Exclusive OR
Decimal | Binary | Operation | ||||||||
162 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | TOGGLE Bit | |
8 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | XOR ( ^ ) | |
170 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | Result |
this could be written as, txtcolor.attr ^= 0x08;
Ain’t that sweet?
So, an INVERSE of an EXCLUSIVE OR would notify you of what bits are the SAME..
Inverse
Decimal | Binary | Operation | ||||||||
211 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | Inverse ( ~ ) | |
44 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | Result |