Posted by Sharvil Nanavati on 2000-09-06
> Thanks, that clears THAT up! When would you use these things? Why
> would
> you want to work with data at the bit level like that?
You would use these things especially when you're dealing with
low-level code. If you look at the DOS interrupt list, you will notice
that some bits in a certain register must be set to perform a specific
task.
Okay, so that was incredibly vague. Here's an example to help out:
DOS Interrupt 0x10, AH == 0, the Set Video Mode function. If bit 7 of
the AL register is set, it prevents the EGA, MCGA, and VGA from
clearing the display. (This information was taken from HelpPC 2.10 by
David Jurgens)
Suppose you wanted to make sure the display doesn't get cleared, you
could use something like this code:
void setmode_no_clear(unsigned char mode)
{
union REGS regs;
regs.h.al = mode | 0x80; /* Sets the 7th bit no matter what */
regs.h.ah = 0;
int86(0x10, ®s, ®s);
}
The binary representation of 0x80 is 10000000. Hence, the 7th bit is
set (recall that we count bits from right to left with the rightmost
bit being referred to as the 0th bit).
When dealing with hardware at a low-level, you must sometimes use ports
to communicate with it. For example, to write data to the keyboard port
0x60, we must make sure the command buffer is empty. This is done by
testing the input from port 0x64 to see if bit 1 is clear.
void check_command_buffer()
{
while((inportb(0x64) & 0x02) != 0) /* while bit 1 is set */
; /* do nothing - just keep checking */
}
Another time you would want to work with things at a bit-level is when
you're using lots of flags. This way, all your flags could be kept in
one byte (if there are <= 8 flags). If there are more flags, they could
be kept in 2 bytes. You're storing 8 pieces of true/false information
in one byte. This is very useful in tile-based games since a lot of the
information for each square of the map is either true or false. Plus,
it makes it more efficient in terms of memory usage.
I would write a lot more, but I really must go - school starts tomorrow
and I have a lot of things to organize!
Cheers,
Sharvil Nanavati
__________________________________________________
Do You Yahoo!?
Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/
Previous post | Next post | Timeline | Home