Bitwise Operations in C

Discussion in 'OT Technology' started by Dysfnctnl85, Oct 24, 2006.

  1. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    I'm having a little bit of trouble with bitwise operations in C and I was wondering if someone could walk me through a problem. Basically, given a 32 bit number (int), how could I select a specific byte (0, 1, 2, 3) of the number?

    The int is signed in this case as well.
     
  2. piratepenguin

    piratepenguin New Member

    Joined:
    Jun 18, 2006
    Messages:
    1,067
    Likes Received:
    0
    Location:
    Ireland
    Logically AND it to another 32 bit int which has the byte you're interested in full of ones and the rest full of zeros. (If you think about that, it makes sense. If you AND something to zero you'll ALWAYS get zero, only when you AND to one do you get one.)

    I wish I could give you the code, but I haven't done this stuff in...ever.
     
  3. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    http://en.wikipedia.org/wiki/Bitwise_operation

    You can use a bit mask.

    number: 0101
    bitmask: 0001

    Now we can use a bitwise AND to discover what the right most bit is. The trick is that the right most bit of the bitmask is 1. Remember, we need two 1's in the bitwise AND operation for the result to be 1. So the bitwise AND can give us what the unknown is.

    After you "extract" a bit, you can do a bitwise shift and do another bitwise AND operation with the same mask to get the next bit.
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Both Post #2 and #3 say the exact same thing, for the record.
     
  5. piratepenguin

    piratepenguin New Member

    Joined:
    Jun 18, 2006
    Messages:
    1,067
    Likes Received:
    0
    Location:
    Ireland
    Yep, they were posted within one minute of eachother.
     
  6. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    OTCcrew FTW :).

    I have a few more I can throw at you if you are hungry...
     
  7. piratepenguin

    piratepenguin New Member

    Joined:
    Jun 18, 2006
    Messages:
    1,067
    Likes Received:
    0
    Location:
    Ireland
    Throw 'em out and we'll C :coolugh:
     
  8. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    I'm a total newbie to C, if it hasn't already hit you in the face. The next problem is to write a function that takes in 4 ints representing dotted-decimal notation and returns the unsigned integer, i.e., (1,2,3,4) returns (16909060).
     
  9. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    shift the first number over 24, second number over 16, third 8 and add the fourth

    Code:
    unsigned int ddn(unsigned int num1, unsigned int num2, unsigned int num3, unsigned int num4)
    {
        return( (num1 << 24) + (num2 << 16) + (num3 << 8) + num4);
    }
    
     
  10. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    Thanks so much!

    I've got K&R, but the bitwise operations are sort of difficult for me to grasp.
     
  11. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
  12. OutHouse

    OutHouse New Member

    Joined:
    Oct 19, 2006
    Messages:
    27
    Likes Received:
    0
    Location:
    MA, USA
    Kernighan and Ritchie
    The C Programming Language
    written by the guys who wrote C (or one of 'em did anyway)
    back in the stone age - when I was programming.
    T'was THE reference for C programming back then.
     
  13. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    Still is :).
     
  14. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    bitwise stuff is difficult to figure out at first because you don't really use it in application programming too much besides some calculation optimization stuff that is sometimes faster. Any kind of low level programming uses bit operations alot. I didn't get good with it until I started doing assembly alot.
     
  15. Virt

    Virt OT Supporter

    Joined:
    Oct 5, 2005
    Messages:
    19,577
    Likes Received:
    43
    .

    Unless you're a computer engineering major, I don't think you'll get too much into bitwise calcs. CompEng makes you get into it early and often though.
     

Share This Page