Thread: bitwise operators

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    bitwise operators

    Hello,

    I have read chapter 2.8.2.3 several times and I still don't get it.

    I know that and is that both must be true.
    Is that with the bitwise and also ?

    and what means XOR, right and left shift and one compliments.

    I have looked at the script but it don't make it more clear.

    Roelof

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Hello,

    I have read chapter 2.8.2.3 several times and I still don't get it.
    Oh my... just wait till chapter 7 ... Just kidding. Bitwise operations are probably the most confusing part of any programming language. Worse than pointers, in my opinion...

    The first thing you need is "truth tables" for the various functions... and yes, these are largely derived from logic gates AND OR NOT and XOR... so they're actually pretty easy once you get the concept.

    For simplicity lets envision a logic "blob" with 2 inputs A and B and 1 output...

    For an AND situation A and B both have to be TRUE (1) before the output is TRUE ... if output is FALSE if either A or B or both are FALSE.
    For an OR situation if either or both of A or B is TRUE the output is TRUE... for the output to be FALSE both A and B must be FALSE.
    For an XOR situation if either A or B is true the output is TRUE... the output will be false if A and B are both FALSE or A and B are both TRUE.

    Now lets picture 8 of these things stacked up beside each other being fed by bytes from the computer...

    Code:
    A =       1 0 1 1 1 0 0 1
    B =       1 1 1 0 1 1 0 0
    
    AND =     1 0 1 0 1 0 0 0
    OR =      1 1 1 1 1 1 0 1
    XOR =     0 1 0 1 0 1 0 1
    This is based on ANDing, ORing or XORing, the individual bits in the bytes. (Of course it works on any integer type variable)

    The most common use is in bit filtering to apply various flags or eliminate unwanted elements from a variable. For example:
    Code:
    // is a number odd?
    int b = A & 1;  // b = 0 or 1, lowest bit is always 1 for an odd number
    
    // make a number odd
    int b = A | 1;  // set the lowest bit on.
    
    // flip even and odd
    int b = A ^ 1; // toggle the lowest bit
    Bit shifting is a bit strange but far simpler... Here you need to picture a clear plastic tube with 7 white balls and one red one... If you put a new white ball in the left end you push everything to the right, effectively moving the red ball over one space... Same from the other end, except now it moves one position left...

    For example...
    Code:
    // merge two bytes in a short int.
    char A = 0xABAD;
    char B = 0xC0DE;
    
    short C = A << 8;  //move A over 8 positions left and assign it to C
        C += B;        // add the value of B to C.
    
    // C = ABADCODE

    Does that help?
    Last edited by CommonTater; 05-16-2011 at 01:17 PM. Reason: Text alignment

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes. "AND" means both must be true.

    if (condition AND condition) ...

    Same with bits. 1 AND 1 ---> 1

    XOR means one or the other exclusively (not both)

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Everything thanks.
    And pointers are not chapter 7 but chapter 5 and I hope I will understand that .
    It also looks very confusing but that is a problem when I get there.

    Roelof

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Sorry I still don't get it.

    I have this question :

    e.How would you turn off all but the low-order four bits in i1
    and I1 is a integer.

    The answer in the book says : i1 & = oxf

    Oke , lets say i1 = 1 binair 00000001

    Then it would be :

    00000001 (1)
    00001111 (f)
    ---------
    00000001 (outcome)

    Then the outcome of and & would be 00000001
    So the last four bits are not turned off.

    Im a right or do i make a big mistake.

    Roelof

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your book is wrong (perhaps it's why you're having so much trouble)

    To clear the bottom 4 bits, it would be
    Code:
    i1 &= ~ 0xf;
    
    00000001 (1)
    11110000 (~ 0xf)
    ---------
    00000000 (outcome)
    ~ is bit-wise not (flip all the bits).
    So regardless of the size of your integer (2, 4, 8 bytes), the top n-4 bits will all be 1's, and the bottom 4 will be zero.
    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.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Maybe find a better online book in the book recommendations.
    But thanks for the help.

    Roelof

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    What do you think about C programming - A modern approach 2nd Edition for a beginner programmer ?

    Roelof

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Sorry I still don't get it.

    I have this question :

    e.How would you turn off all but the low-order four bits in i1
    and I1 is a integer.

    The answer in the book says : i1 & = oxf

    Oke , lets say i1 = 1 binair 00000001

    Then it would be :

    00000001 (1)
    00001111 (f)
    ---------
    00000001 (outcome)

    Then the outcome of and & would be 00000001
    So the last four bits are not turned off.

    Im a right or do i make a big mistake.

    Roelof
    Read the test question again... it says turn off all but the low order 4 bits... that is eliminate bits 5 through n... The book answers with i1 & 0xF which is correct.

    Try working the problem with higher values....
    Code:
    1100 0101  
    0000 1111
    ----------
    0000 0101
    Get it now?
    Last edited by CommonTater; 05-17-2011 at 08:08 AM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Hello,

    Maybe find a better online book in the book recommendations.
    But thanks for the help.

    Roelof
    Or you could just read more carefully

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I get it but still I try now the other book.
    But still many many thanks.

    Roelof

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries, my friend... Good luck with the new book...

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Except that the book wasn't wrong to begin with.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Except that the book wasn't wrong to begin with.
    Maybe so... but I got the impression Roelof wasn't too happy with the way it was teaching either...
    If you think about it hitting someone with Bitwise operations in chapter 2 is kinda nasty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise operators
    By scrapper in forum C Programming
    Replies: 21
    Last Post: 02-17-2005, 05:38 PM
  2. Bitwise operators
    By djarian in forum C Programming
    Replies: 3
    Last Post: 10-10-2003, 10:33 AM
  3. Bitwise Operators....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 21
    Last Post: 04-09-2003, 06:45 AM
  4. What's the use of bitwise operators?
    By seh_hui in forum C Programming
    Replies: 8
    Last Post: 01-13-2002, 05:59 AM
  5. bitwise operators
    By Qasim in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 09:23 AM