Thread: Bitwise -- b&01

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Exclamation Bitwise -- b&01

    Hi ,

    Could you please explane to me the statment " b&01"

    b=4=100 (in binary)
    why 100&01 it's 0? ( and not 100)

    Code:
    int b=4;            
     if (b&01)             
            {
             //do somthing   
    
            }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Here's the the truth table for &:
    Code:
    a | b | a & b
    1 | 1 |   1
    1 | 0 |   0
    0 | 1 |   0
    0 | 0 |   0
    & is a bit operator thus you have to combine the equivalent "digits":
    Code:
    1 0 0
          &
      0 1
    -----
    0 0 0
    And numbers with a leading 0 are treated as octal values in C (not relevant here, but probably in other problems).

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    Hi Andreas

    Q -1 what about regarding the 1 degit in red?
    With what do I multipole the red 1 ?
    1 0 0

    &

    0 1

    Q -2
    10110 ---------> ( 22 desimal)
    &
    000 ---------> ~( 7 desimal)
    -------
    10000 ---------> (16 desimal)

    22&~7=16 in the example the 10 left digits are remain as they are , could you please explane?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You fill up the left side with zeros. It's the same principle with decimal numbers:

    Code:
    1 2 3
         +
        4
    -----
    1 2 7
    is the same as

    Code:
    1 2 3
         +
    0 0 4
    -----
    1 2 7
    Bye, Andreas
    Last edited by AndiPersti; 04-22-2013 at 10:52 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    22&~7=16 in the example the 10 left digits are remain as they are , could you please explane?
    ~7 does not equal zero.
    It gives a value for which all but the last 3 bits are ones.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise more than 8? or less than 8?
    By tennisstar in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2012, 07:05 AM
  2. Help with bitwise AND
    By tehjojo in forum C Programming
    Replies: 1
    Last Post: 04-03-2010, 02:23 PM
  3. bitwise? am i right?
    By skiz in forum C Programming
    Replies: 10
    Last Post: 03-11-2009, 11:37 PM
  4. Bitwise OR
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2005, 02:23 AM
  5. bitwise help
    By linuxdude in forum C Programming
    Replies: 8
    Last Post: 11-20-2003, 08:37 PM