Practical usage of bitwise operators.

J

jas_lx

The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.

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.

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.

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.

So, does anyone have good practical examples of using bitwise
operators?
 
R

Raymond Martineau

The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.
....

So, does anyone have good practical examples of using bitwise
operators?

Bitwise operators can be practically used to pack multiple boolean values
into a single char (or even an int on some playforms.) Some games have
been known to do this. As an example, one game uses the following:

/*
* Chest trap flags (see "tables.c")
*/
#define CHEST_LOSE_STR 0x01
#define CHEST_LOSE_CON 0x02
#define CHEST_POISON 0x04
#define CHEST_PARALYZE 0x08
#define CHEST_EXPLODE 0x10
#define CHEST_SUMMON 0x20

If a player attempts to open a chest, the bitwise operators are used on one
of the fields describing the object to check if these traps will activated.
In theory, any combination can result depending on where the chest was
found.
 
J

Jens.Toerring

jas_lx said:
The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.
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.
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.
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.
So, does anyone have good practical examples of using bitwise
operators?

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
 
G

Gregory Toomey

jas_lx said:
The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.

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.

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.

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.

So, does anyone have good practical examples of using bitwise
operators?

Have a look at the well-known Crafty chess engine
http://www.crafty-chess.com/

Its
- written in C
- represents pieces (eg white pawns) as 64 bit integers, one bit for each
square
- conputes moves using bit operations
- is just about all bit fiddling

gtoomey
gtoomey
 
J

Jack Klein

The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.

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.

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.

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.

So, does anyone have good practical examples of using bitwise
operators?

Want a real world example? Take a look at
http://jk-technology.com/C_Unleashed/code_list.html, which contains
GPL source code from a chapter I wrote in a book about C some years
ago.

The first 6 files listed, Chap18.txt through lj300.c use quite a bit
of bitwise operations. They include code for encoding a file
representing a black-and-white bit-mapped image into standard T.4
encoding for a FAX machine, and the reverse. If you want the text of
the article you'll have to buy a copy of the book, because the
publisher owns that, but the files on this page are all GPL.

Realistically, in addition to dealing with hardware devices, bitwise
operations are extensively used in data compression, data encryption,
data checking, and all sorts of image processing, including JPEG and
MPEG formats.

There are many other uses as well.
 
K

Keith Thompson

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;

I'm afraid that's not a good example. If is_used is intended to store
only a single boolean value, a better way to negate it is:

is_used = !is_used;

Your "is_used ^= 1;" only toggles the low-order bit. If is_used
happens to have a value other than 0 or 1, the logically true value
will remain logically true.

If you can guarantee that you'll never store a value other than 0 or 1
in is_used, you can get away with operating on just the low-order bit,
but that's a difficult (and unnecessary) guarantee to make. Something
as innocuous as

is_used = isalpha(some_char);

can break it.
 
J

jas_lx

Wow! These are all great examples! Thanks everyone for the responses.

The AI_Command example for writing to a board register is awesome, as
are the chess game, the video game, and the example from "C
Unleashed". The last one I'm going to try to compile, alter (for
experimentation), and compile again, then run it - should be fun!
Thanks everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top