# Bitwise operations

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-23-2007
sh3rpa
Bitwise operations
anyone no of any good sites on bitwise operations not directly relating to game programming?
• 09-23-2007
Salem
Meaning what exactly?

I don't think the operators themselves care whether it's a game program or an operating system (for example). They do the same thing in either case.
• 09-24-2007
zsk_00
• 09-24-2007
DougDbug
Bitwise operations are farily simple. The tutorial may cover everything! The thing that makes it hard for beginners is that cin and cout don't directly support binary, so you can't easily "see" what you are doing.

You can write your own code to display (and input) binary numbers, or you can use <bitset> to display numbers in binary format, and strtoul() to input a c-style string that represents a binary number (or a number in almost any other base).

However, most real-world programs that use bit manipulation use cin and cout with hexadecimal representation. It is very easy to convert between hex and binary. (You can learn to do it in your head.)

Bitwise operations are most-often used when you are programming "close to the hardware" (drivers or firmware), or with assembly-language programming. The state of one particular bit might represent the on/off state of an LED, or the on/off state of a switch, etc.

And, we usually don't care about the numerical value of the binary number.... We are only concerned with the value of a particular bit, or the "bit pattern". We don't care that 1111 (binary) equals 15 (decimal), or F (hex). We just want to know that all of the bits are "set".
• 09-25-2007
sh3rpa
I guess it's not the actual operations that confuse me, it's when do you use them.

Bitwise operations are used to talk directly to hardware? I guess I'm just used to dealing with a layer of abstraction,.

So, maybe an easy example of how to 'talk' to the hardware or 'addresses' so I can see how it's done, if at all possible?

Many thanx!
• 09-25-2007
CornedBee
No. They are used (among other situations) when talking to hardware; they are not the means by which this is done. (It is done through the OS's driver interface.)
• 09-25-2007
brewbuck
I'd say the following are the MOST common uses of bitwise operators, although there are certainly others:

1. Storing binary flags inside integer variables. A single int can hold many flags (on many systems, up to 32 of them). You could do this with 32 separate variables, but using a single int is much cheaper, and it allows you to do more complex things to the flags by using bitwise operators.

2. Talking to hardware. Hardware often takes commands in the forms of single bytes or words, each bit of which represents some flag or status to be set/retrieved from the hardware. You need bitwise operators to send and interpret these results.

3. Accessing the individual bits of a 1-bit bitmap image.

Other fun uses include bitboards in AI chess programming, encryption, etc...
• 09-25-2007
CornedBee
Don't forget reading tightly-packed file formats like SWF.
• 09-25-2007
hk_mp5kpdw
#1 - Do you want to test if a number is odd/even? You can do this using bitwise and to test:
Code:

```unsigned int num; ... if( num & 1 ) {     // It's odd } else {     // It's even }```
#2 - You can use the bitwise shifting operators to efficiently multiply/divide integer values by powers of 2:
Code:

```num <<= 1;  // Same effect as num *= 2; num <<= 3;  // Same effect as num *= 8; num >>= 4;  // Same effect as num /= 16;```
This shift operation can be done very quickly and is efficient (no need to actually call an expensive multiply or divide operation) although a good compiler can do the same thing when converting your code if the value being multiplied or divided by is a const/literal (and a power of 2).

#3 - You can use bitwise operators to set/clear/check the values in a variable that may be used as a flag (as mentioned by brewbuck in point #1):
Code:

```const unsigned int ERR_FLAG1 = 1;  // 0000 0000 0000 0001 = 0x1 const unsigned int ERR_FLAG2 = 2;  // 0000 0000 0000 0010 = 0x2 const unsigned int ERR_FLAG3 = 4;  // 0000 0000 0000 0100 = 0x4 const unsigned int ERR_FLAG4 = 8;  // 0000 0000 0000 1000 = 0x8 const unsigned int ERR_FLAG5 = 16;  // 0000 0000 0001 0000 = 0x10 const unsigned int ERR_FLAG6 = 32;  // 0000 0000 0010 0000 = 0x20 ... etc, etc, etc. unsigned int flag = 0; ... flag |= ERR_FLAG3;  // Sets the ERR_FLAG3 value stored in variable "flag" flag &= ~ERR_FLAG1;  // Clears the ERR_FLAG1 value stored in variable "flag" if( flag & ERR_FLAG2 ) {     // ERR_FLAG2 value stored in variable "flag" was turned ON } else {     // ERR_FLAG2 value stored in variable "flag" was turned OFF } if( flag & ERR_FLAG5 && !(flag & ERR_FLAG1) ) {     // ERR_FLAG5 was turned ON and ERR_FLAG1 was turned OFF } else {     // Either ERR_FLAG5 was OFF or ERR_FLAG1 was ON }```
The single "flag" variable above can be used to store many different settings which can be individually checked/set/cleared.
• 09-25-2007
CornedBee
But #1 and #2 should really, really be left to the compiler. Especially as #1 doesn't necessarily work for negative numbers: not on 1's complement systems. Do a &#37;2, and let the compiler optimize it if it decides the platform can handle it.
• 09-25-2007
sh3rpa
if found this example at http://www.fredosaurus.com/notes-cpp...s/bitops.html:
Code:

```int age, gender, height, packed_info;     . . .  // Assign values // Pack as AAAAAAA G HHHHHHH using shifts and "or" packed_info = (age << 8) | (gender << 7) | height; // Unpack with shifts and masking using "and" height = packed_info & 0x7F;  // This constant is binary ...01111111 gender = (packed_info >> 7) & 1; age    = (packed_info >> 8);```
which seems to explain a little bit.

I don't however understand the line:
Code:

`height = packed_info & 0x7F;`
Is this good practice? to purposely mask numbers in one larger number? is this much more efficient than not dong this?
• 09-25-2007
sh3rpa
also, is 2's complement the standard? how do you know what binary system is being used?
• 09-25-2007
Prelude
>also, is 2's complement the standard?
No, but it tends to be the most common.

>how do you know what binary system is being used?
```Uint32 ColourBlend( Uint32 rgb_1, Uint32 rgb_2, Uint8 alpha_1 ) {         Uint8 alpha_2 = 256 - alpha_1;                 Uint32 rb_1 = ((rgb_1 & 0x00FF00FF) * alpha_1) & 0xFF00FF00;         Uint32 g_1  = ((rgb_1 & 0x0000FF00) * alpha_1) & 0x00FF0000;         Uint32 rb_2 = ((rgb_2 & 0x00FF00FF) * alpha_2) & 0xFF00FF00;         Uint32 g_2  = ((rgb_2 & 0x0000FF00) * alpha_2) & 0x00FF0000;                 rb_1 += rb_2;                        g_1  += g_2;                 return (rb_1 | g_1) >> 8; }```