jas_lx <jas_lx@yahoo.com> wrote:[color=blue]
> The basic understanding of what bitwise operators (& ^ | >> << ) comes
> fairly simple, as long as one has a fundamental understanding of bits,
> bytes and binary.[/color]
[color=blue]
> Having done some Win32 programming in straight C, the Win32 API sends
> windows messages to applications with various settings stored in bit
> fields, in which case changing the settings of the message is a matter
> of using the bitwise operators. This was a good practical example of
> using bitwise operators.[/color]
[color=blue]
> However, in all of my programming endevours and studies (including the
> K&R book, Data Structures books, Stroustrup's book, Petzold's book, and
> various Linux programming books), I have yet to come across any really
> good, practical examples of using bitwise operators.[/color]
[color=blue]
> With C, of all languages, it helps tremendously to have practical,
> meaningful examples of using the various constructs. Let's face it, C
> syntax is intitially non-intuitive, and it's paradigms (pointers,
> dynamic memory allocation, bitwise operators, and the like), can be
> confusing at times and a source of hard to detect bugs, even for
> experienced programmers.[/color]
[color=blue]
> So, does anyone have good practical examples of using bitwise
> operators?[/color]
There are a lot of examples where using them is quite convenient.
E.g. toggling a bit of an unsigned char that's only used as a
boolean value can be quite simple by writing e.g.
is_used ^= 1;
instead of
if ( is_used )
is_used = 0;
else
is_used = 1;
or, a bit shorter,
is_used = is_used ? 0 : 1;
They are, as you write, often used to store a set of binary states
in a single variable. But most often bitwise operators are used (at
least to my of course limited experience) when you have to deal with
hardware on a low level. Then they are extremely handy and much
easier to read and understand than a replacement by with lots of
multiplications, modulo operations etc. You often have to set or
reset a single or a small number of bits in values that you read
from hardware registers and then write them back to initiate a
certain action of a device. Here's a line, more or less picked at
random from a driver I wrote for D/A-converter card
AI_Command_1 &= ~ ( AI_SC_Arm | AI_SI_Arm | AI_SI2_Arm );
AI_Command_1 is a variable where I store the value I will have to
write later to a register of the board (the AI Command Register 1)
and which I use to keep a track of what the driver has written to
it before to be able to remember in which state the board is. The
things on the right side are defined (to make the program readable)
as
#define AI_SI2_Arm ( 1 << 12 )
#define AI_SI_Arm ( 1 << 10 )
#define AI_SC_Arm ( 1 << 6 )
So AI_SI2_Arm stands for the bit 12 of the register etc. And the
whole line above is meant to reset (switch off) these bits in the
representation of the (16-bit wide) register. Later I am going to
write that value to the register to stop the board, which is just
doing a data acquisition (AI stands for analog input and the board
is repeatedly converting an input voltage at a certain rate, by
resetting the bits the frequency gets set to 0 Hz and thus the con-
version is effectively stopped).
If you look at such kind of programming you will find lots and lots
of lines like that, basically whenever you have to deal with the
hardware. If you look at any documentation about the hardware level
programming of a device you will immediately see what the bitwise
operators are good for. An example where you can see them used in
lots of places are e.g. the low level drivers in the Linux kernel.
Regards, Jens
--
\ Jens Thoms Toerring ___
Jens.Toerring@physik.fu-berlin.de
\__________________________
http://www.toerring.de