"Junmin H." <ti*****@gmail.comwrote in message
news:fc**********@aioe.org...
Hello, all. I never use them when I am programming.
But i have been reading codes that using them.
When should I use them? Why not use char/short/int/long?
Is it very important to know them well? Because it's binary,
I found that it's a little difficult to understand then decimal.
They are used quite a lot in everyday C programming.
For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
provide a very cheap modulus operation.
Also, sometimes you need to treat memory as a stream of bits, not a stream
of bytes. This is typical for compression programs, for example. The only
way to extract bits is to use the operators to mask out the bits you don't
want, shift them, and recombine.
They are also not infrequently used to pack two or more pieces of
information into a single variable. Fairly commonly you will see functions
take flags, combined with the OR operator.
Whilst you can do without them, they are extremely useful, and also one of
the fundamental operations the processor can perform. Addition is performed
with a cascade of XOR and other operations, internally, for instance.
01 + 01 = 10 (binary addition)
XOR the lowest bit to get the lowest digit of result.
AND the two bits to get the carry.
Repeat with carry + one bit in the next place to get a half result
Repeat with half result and other bit to get the next digit
OR the two carrys from previous two operations to get the carry.
Repeat steps until you have all the binary digits, plus a carry.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm