Thread: Bitwise operators

  1. #1
    Unregistered
    Guest

    Bitwise operators

    I've seen what they are supposed to do, and some explanation as to what they can be used for, but I'm still lost.

    What do they do and how do you know when to use them. When you do use them what are the results? For what purpose in normal coding as well as neat tricks are they used? I've read books and the basic books just tell you what they are, then the advanced books assume you know them in and out, nobody seems to be able to explain this so that I can understand.

    Thanks a bunch

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    197

    a similar thread exists

    seh_hui asked the same question on the thread "What's the use of bitwise operators?" on December 4th 2001.
    There are a few good answers (I answered, too!)

    klausi
    When I close my eyes nobody can see me...

  3. #3
    Unregistered
    Guest
    I read that, it gave a bunch of vague uses and a couple of good tricks, but nobody explained what they do or why
    For example,
    if(x & 1)
    printf("odd");

    Why does this work, how does it work? I'm looking for someone to explain to me why they are used, how they are used, and what they actually do without using the vague explanation that all books give. The second two questions are far more important to me than the first at the moment though.

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    i dont know fully what bitwise operators are available - i know OR and XOR are , also AND . thaye combine values, search google for their truth tables.
    Monday - what a way to spend a seventh of your life

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    | - OR
    & - AND
    ^ - XOR
    ~ - Unary (opposite)
    << - shift left
    >> - shift right

    OR is fairly simple:
    Code:
    (1 | 2)
    
       00000001
    |  00000010
    -----------------
       00000011
    It basically just adds the binary of the numbers. It doesn't actually add the numbers though, remember that.

    AND is just as easy:
    Code:
    (1 & 3)
    
       00000001
    &  00000011
    ------------------
       00000001
    See what it did? everywhere there was a 1 in the first one and the second, there was a 1 in the resulting number.

    XOR is trickier:

    Code:
    (1 ^ 3)
    
       00000001
    ^  00000011
    ------------------
       00000010

    Anywhere there was a 1 in both numbers, it became a zero.

    UNARY is the easiest. It basically means binary opposite:


    Code:
    (~1)
    
    ~0000000001 = 1111111110
    << and >> (shift left and right) are incredibly simple, and used the most:

    000000010 >> 1 = 000000001
    000000001 << 1 = 000000010

    It basicall goes like this: number >> Amount to shift by
    It moves the numbers down by the number you place to the right.

    >> Is actually the quickest way to divide by 2
    << Is actually the quickest way to multiply by 2

    Another thing about shifts, is that the numbers don't carry! soo:

    000000001 >> 1 = 00000000, NOT 10000000


    And remember I wrote the numbers out in binary instead of the actual numbers you'd put in there. ( 000000011 is 3, 0000000010 is 2, and 000000001 is 1 binary) So remember, you don't write the numbers in binary, you write them in decimal to use the bit operators.


    Any questions, just ask me...
    Last edited by -KEN-; 01-13-2002 at 01:35 PM.

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    To fully understand how bitwise operators work, you should have an understanding of bits in genereal. Read this binary primer (or try here). After gaining an understanding of binary digits from one of the links, you should be ready to understand some basic bit math.

    Binary AND (&)
    The binary AND (&) compares two bits and returns true (1) if both bits are set to true.
    • 0 & 0 = 0
      1 & 0 = 0
      0 & 1 = 0
      1 & 1 = 1


    Binary OR (|)
    The binary OR (|) compares two bits and returns true if either or both bits are set to true.
    • 0 | 0 = 0
      1 | 0 = 1
      0 | 1 = 1
      1 | 1 = 1


    Binary Exclusive OR (XOR) (^)
    The binary exclusive OR (^), also known as XOR, compares two bits and returns true if only one of the bits are set to true.
    • 0 ^ 0 = 0
      1 ^ 0 = 1
      0 ^ 1 = 1
      1 ^ 1 = 0


    In C, you are normally applying binary operators against a group of bits organized as a byte or multiple bytes. For example, if we took the numbers 5 and 3 and XOR'd them together, it would look like this:
    • 00000101 (5)
      00000011 (3)
      -------------
      00000110 (6)


    Using bitwise AND to see if a number is odd is done with:
    Code:
    if ( a & 1 )
      printf("Odd");
    Assume the variable 'a' equals thirteen:
    • 00001101 (13)
      00000001 ( 1)
      -------------
      00000001 ( 1)

    For every odd number, the lowest order bit will be set true. For the number 1 (00000001), the only bit set is the low order bit. If we AND them together, we can see if the number in question has the low order bit set, and if it does, the number is odd. This works because the expression "a & 1" will return 1 (TRUE) if the low order bit is set and 0 (FALSE) if the bit is not set (clear).

    We can also see if a character is uppercase or lowercase because, in ASCII, the only difference between an upper or lower case letter is whether the bit that represents 32 is true (it is true for lowercase letters). Let's take the letters 'c' and 'C'. If we AND either with 32, we can see if they are upper or lower case:
    • 01000011 (C == 67)
      00100000 (32)
      -------------
      00000000 (0 == FALSE)

      01100011 (c == 99 )
      00100000 (32)
      -------------
      00100000 (32 == TRUE)


    So, the expression "some_char & 32" is TRUE if the variable 'some_char' holds a lowercase letter. Does this help at all?
    Last edited by Deckard; 01-13-2002 at 01:41 PM.
    Jason Deckard

  7. #7
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Oops, -KEN- was too fast for me
    Jason Deckard

  8. #8
    Unregistered
    Guest
    Oh yea, that helps a lot. Thanks KEN and Deckard
    I'm going to read that page you gave me and then try to understand better, is it okay if I ask you more questions as I figure out how to word them?

  9. #9
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Unregistered
    is it okay if I ask you more questions as I figure out how to word them?
    Absolutely. That's why we keep coming back :)
    Jason Deckard

  10. #10
    Unregistered
    Guest

    Bitwise operators

    very very cool. I was having problems with the bitwise operators as well.

    I was wondering how you would do a XAND on a bit?

    would

    a = ~a
    a = &a

    work? and am i thinking of bitwise operators properly now?

    Thanks again

  11. #11
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    What exactly would an exclusive AND do?

  12. #12
    Unregistered
    Guest

    Bitwise operators

    I was thinking that an XAND could be used in something like a netmask for internet applications the got point to point.. or used to catch broadcast messages from a subnet like a DHCP or bootp signal.

  13. #13
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    That's not really what I meant...

    An XOR is true when A or B is true, exclusively (when onely one of them is on). So, how would XAND work? If it's true exclusively when A and B are true, then it's an AND - it's it's true when neither are true, then it's a NAND? When would an XAND be true?

  14. #14
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    An AND is an exclusive operator since it's only true when both A and B are true. So there is no such as a XAND.

  15. #15
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > So there is no such as a XAND.

    That's kinda what I was trying to get Unreg. to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM