I know that ~ means not, but I don't know how it works in the following code. Like the comment says, when tab is pushed, it increments the cursor by 8, but makes sure its divisible by 8. I don't understand the why the ' & ~(8 - 1) ' part works. For the full code, go here: http://osdever.net/bkerndev/Docs/printing.htm
1 2 3 4 5 6
/* Handles a tab by incrementing the cursor's x, but only
* to a point that will make it divisible by 8 */
elseif(c == 0x09)
{
csr_x = (csr_x + 8) & ~(8 - 1);
}
~ is a binary not (I've also seen it called the "complement" operator). It effectively XORs all bits with 1 (toggling their state)
~(8-1) evaluates to ~7... which evaluates to 0xFFFFFFF8 because 7 has only the low 3 bits set, 0xFFFFFFF8 has all bits set except the low 3.
combining this with an AND operation: & ~(8 - 1), and you are effectively clearing the low 3 bits. Therefore the following statement:
csr_x = (csr_x + 8) & ~(8 - 1);
adds 8 to csr_x, then clears the low 3 bits.
If it's unclear as to what "clear the low 3 bits" means -- to fully explain it I'd have to get into how integers are represented in binary. Basically it "rounds down" to the next multiple of 8. ie.. .values 0-7 become 0... values 8-15 become 8, 16-23 become 16, etc.
Combine that round-down action with the +8, and it's basically "rounding up" to the next multiple of 8