I'm a veteran programmer - been programming in C for about 20 years now. I'm familar with C bitwise operations: Shifting, |, &, XOR etc.
In these years i've programmed, i've never written code thats utilized these operations. Can someone explain or cite an example in Osdev where I WILL have to use bitwise operations to get the job done.... Someone help me solve this mystery thats been bothering me the last couple decades? Thanks.
Bitwise Operations In C
Hi,
One of the most useful times for using bitwise | and & are when you are setting / checking / clearing flags in an integer value. Bit shifting can also make things clearer here. Non-OS dev sepcific Example:
OK - I know this is a silly example, but it has uses in some situations. When talking about OS dev, there are some much more relevant examples. To get the address of a page table, for example:
...because here, we want to clear the lowest 12 bits of the page directory entry. Also, if you want to check/set/clear flags in CPU control registers, you will use bitwise &, |, and ~ to do this based on a previous register read.
XOR also has some implications for basic encryption. XORing a number twice with the same rvalue will return you to the original value.
Cheers,
Adam[/code]
One of the most useful times for using bitwise | and & are when you are setting / checking / clearing flags in an integer value. Bit shifting can also make things clearer here. Non-OS dev sepcific Example:
Code: Select all
#define FLAG_ERROR 1
#define FLAG_CARRY (1 << 1)
#define FLAG_ZERO (1 << 2)
//...and so on...
int myflagvariable = 0;
int mytestvalue = 0;
void an_error_happened()
{
/* set some flags */
myflagvariable |= FLAG_ERROR;
if(!mytestvalue) myflagvariable |= FLAG_ZERO;
}
void clear_error()
{
/* clear a flag */
myflagvariable &= ~FLAG_ERROR;
}
void checkflagsforsomereason()
{
if (myflagvariable & FLAG_ERROR) printf("An Error Happened");
if (myflagvariable & FLAG_CARRY) printf("Carry in last calculation");
if (myflagvariable & FLAG_ZERO) printf("NULL result");
}
Code: Select all
unsigned long *PT = (cast)(PD[PDE] & ~0xFFF);
XOR also has some implications for basic encryption. XORing a number twice with the same rvalue will return you to the original value.
Cheers,
Adam[/code]
-
Silver Blade
- Posts: 8
- Joined: Wed Jan 02, 2008 1:19 pm
- Location: Oxfordshire, UK
& (and) is great for "cleaning" flags, to ensure certain bits are NOT set before you pass them to an API or hardware component.
For example, this would make certain the highest bit were clear in variable "foo":
foo &= 0x7f
You can also use this to un-set/clear bits - by setting all bits apart from the one you want to clear.
| (or) is probably the most useful, as you can set individual bits whilst maintaining the rest of the flags in a variable. The following example would ensure the highest bit in variable "foo" is set. All other bits are preserved:
foo |= 0x80
^ (xor) I've never really used, so don't really have much examples to give of how it's useful.
NOT just inverts all bits, which I find useful when dealing with IRQ masks. In my case, I take the IRQ number, and in the case of it being lower than 8, to enable an IRQ I shift the value 1 by the number of the IRQ - which results in a value where the appropriate bit is set, and all other cleared.
Then I NOT the value so that in fact all bits are set, aside from the one which represents the IRQ.
Finally, this value is ANDed with the existing PIC mask, before being written back to the PIC.
This means if all IRQs are masked and IRQ 0 is being un-masked, it would AND together 0xff and 0xfe and result in 0xfe (lowest bit cleared).
For example, this would make certain the highest bit were clear in variable "foo":
foo &= 0x7f
You can also use this to un-set/clear bits - by setting all bits apart from the one you want to clear.
| (or) is probably the most useful, as you can set individual bits whilst maintaining the rest of the flags in a variable. The following example would ensure the highest bit in variable "foo" is set. All other bits are preserved:
foo |= 0x80
^ (xor) I've never really used, so don't really have much examples to give of how it's useful.
NOT just inverts all bits, which I find useful when dealing with IRQ masks. In my case, I take the IRQ number, and in the case of it being lower than 8, to enable an IRQ I shift the value 1 by the number of the IRQ - which results in a value where the appropriate bit is set, and all other cleared.
Then I NOT the value so that in fact all bits are set, aside from the one which represents the IRQ.
Finally, this value is ANDed with the existing PIC mask, before being written back to the PIC.
This means if all IRQs are masked and IRQ 0 is being un-masked, it would AND together 0xff and 0xfe and result in 0xfe (lowest bit cleared).
Seems a little odd for someone who's been programming C for 20 years never to have experimented with this stuff ... 
Assuming that you are strictly doing C, and not dealing with ASM much at all, then your compiler is doing an enormous number of bitwise operator optimizations on your C code, for +-/* && ||
but you don't really have to know about any of it, unless you are trying really hard to write highly optimized, high speed, tiny code.
The one big area that hasn't been mentioned yet is the concept of "handles". For an example from my kernel, I have things called "movable buffers." They are stored in a table, and are referenced through an index into this table. The values in the table are a pointer to the current location of the buffer, an "allocated" flag, and the size of the buffer. Buffers are multiples of 8K in size, so the bottom 13 bits of the pointer are always 0. Lengths of buffers are always less than 4096 8K pages, so I can store the length of the buffer in the bottom 12 bits. I have one bit left for the allocation flag. So, when accessing a movable buffer, I do:
(And clearly, when I put a handle together to store, I need to OR all these values back together again.)
Now, of course it is possible to simply store the pointer, pagecount, and allocation flag in 3 separate variables in a big table. If you do it in a reasonably intelligent way, you will only waste 50% more space than I do.
Another few places where bitwise operations are vital are in drawing monochrome graphics, and in data compression algorithms -- especially the "arithmetic" ones.
Oh ... and XOR is very useful for doing bitwise toggles. 0^1 = 1, and 1^1 = 0. So if you loop on foo[bar], and inside your loop you do bar^=1 ....
Assuming that you are strictly doing C, and not dealing with ASM much at all, then your compiler is doing an enormous number of bitwise operator optimizations on your C code, for +-/* && ||
but you don't really have to know about any of it, unless you are trying really hard to write highly optimized, high speed, tiny code.
The one big area that hasn't been mentioned yet is the concept of "handles". For an example from my kernel, I have things called "movable buffers." They are stored in a table, and are referenced through an index into this table. The values in the table are a pointer to the current location of the buffer, an "allocated" flag, and the size of the buffer. Buffers are multiples of 8K in size, so the bottom 13 bits of the pointer are always 0. Lengths of buffers are always less than 4096 8K pages, so I can store the length of the buffer in the bottom 12 bits. I have one bit left for the allocation flag. So, when accessing a movable buffer, I do:
Code: Select all
handle = mbuf_table[index];
mb_ptr = handle & 0xffffe000;
mb_pages = handle & 0xfff;
mb_alloc = handle & 0x1000;
Now, of course it is possible to simply store the pointer, pagecount, and allocation flag in 3 separate variables in a big table. If you do it in a reasonably intelligent way, you will only waste 50% more space than I do.
Another few places where bitwise operations are vital are in drawing monochrome graphics, and in data compression algorithms -- especially the "arithmetic" ones.
Oh ... and XOR is very useful for doing bitwise toggles. 0^1 = 1, and 1^1 = 0. So if you loop on foo[bar], and inside your loop you do bar^=1 ....
Hi,
Cheers,
Brendan
Wouldn't a reasonably intelligent way be to use a structure of bitfields, so it (hopefully) still only consumes 32-bits, but so you also don't need to do the bitwise stuff manually?bewing wrote:Now, of course it is possible to simply store the pointer, pagecount, and allocation flag in 3 separate variables in a big table. If you do it in a reasonably intelligent way, you will only waste 50% more space than I do.
Code: Select all
typedef struct {
unsigned int mb_pages : 11;
unsigned int mb_alloc : 1;
unsigned int mb_ptr : 20;
} MB_STRUCT;
MB_STRUCT mbuf_table[MAX_ENTRIES];Code: Select all
handle = &mbuf_table[index];
mb_ptr = handle->mb_ptr;
mb_pages = handle->mb_pages;
mb_alloc = handle->mb_alloc;Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
