Bitwise operations

This is a discussion on Bitwise operations within the C++ Programming forums, part of the General Programming Boards category; anyone no of any good sites on bitwise operations not directly relating to game programming?...

  1. #1
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    Bitwise operations

    anyone no of any good sites on bitwise operations not directly relating to game programming?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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".

  5. #5
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    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!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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...

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Don't forget reading tightly-packed file formats like SWF.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    #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.
    Last edited by hk_mp5kpdw; 09-25-2007 at 01:20 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    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?

  12. #12
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    also, is 2's complement the standard? how do you know what binary system is being used?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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?
    Read the manual for your system?
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I always use an unsigned type for array/vector indices, and for these it's safe to use the bitwise forms (hk_mp5kpdw should have pointed out that the "unsigned" part of the declaration is important, and the two's complement/ones' complement/sign-magnitude issue only applies to signed types).

  15. #15
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    32 bit colour blending:
    Code:
    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;
    }
    Last edited by mike_g; 09-25-2007 at 02:38 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operations to Read ON/OFF Bits
    By pseudonoma in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 02:15 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  4. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-27-2002, 11:46 PM
  5. bitwise operations
    By bukko in forum C Programming
    Replies: 3
    Last Post: 10-06-2001, 06:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21