Thread: bits in C

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    bits in C

    I do not know how to refer to bits in C and i need an example.Do i refer as char or as int.I do not know basic commands in bits.
    I just need an example...

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Last edited by zacs7; 07-10-2007 at 06:51 AM.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You should read up about 'bitwise operators' in C. There should be a tutorial here about it.

    For example if you want to check if the 32 bit in a byte is on you could use the bitwise 'and' opetator:
    Code:
    if(somenumber & 32)
    For setting a bit I would check if its set first, if not then just add the bit value to it.
    Code:
    if(!(somenumber & 32))somenumber+=32;
    This may not be the best way to do this tho.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by mike_g View Post
    For example if you want to check if the 32 bit in a byte is on you could use the bitwise 'and' opetator:
    Not to be pedantic, but a byte doesn't have a 32nd bit on most architectures.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The tutorial covers all that in depth, and explains there's 8 bits to a byte, usually
    Last edited by zacs7; 07-10-2007 at 07:28 AM.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Er... I said 32 bit, not 32nd, but I guess it could seem confusing. I usually talk like that anyway with binary stuff and break ints down into octects. Plus it meant that I dident have to go into bitshifting with my explanation. But yeah, i'm sure the tutorial is better than my vague explanation.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ohh, I see where your coming from. Checking if the 32nd bit is on in a set of bytes, ie a byte (if you break it down)?

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For setting a bit I would check if its set first, if not then just add the bit value to it.
    Code:
    if(!(somenumber & 32))somenumber+=32;
    This may not be the best way to do this tho.
    Or or:
    Code:
    somenumber |= 32;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, ORing its better. I forgot that

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    Not to be pedantic, but a byte doesn't have a 32nd bit on most architectures.
    He said "32 bit" not "32nd bit." The 32 bit is the bit with value 32, in other words the fifth bit.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    That bit would still be in one byte, as 32 in this case would be bit 5 (starting from bit 0).

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I get it now.... I just never think of the 5th (or 6th) bit as the "32 bit". lol....

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I get it now.... I just never think of the 5th (or 6th) bit as the "32 bit". lol....
    Me either. I would certainly use hex to write the same expression:
    Code:
    if (somenumber & 0x20)
    Or do a bit shift.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging bits
    By Drac in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2008, 06:13 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. byte is equal 8 bits???
    By Micko in forum C Programming
    Replies: 3
    Last Post: 10-15-2004, 10:12 AM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM