bitwise IF statement

This is a discussion on bitwise IF statement within the C Programming forums, part of the General Programming Boards category; Hello! I have a simple question here: How does an IF statement react on bitwise commands like this: a = ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    bitwise IF statement

    Hello!

    I have a simple question here:
    How does an IF statement react on bitwise commands like this:
    a = 0x07;
    b = 0x02;

    if(a&b)
    DoSomething();

    Is the IF statement TRUE or FALSE?

    Tnx
    /D

  2. #2
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    this statement would be true:
    since 0x7 = 0111 bin
    and 0x2 = 0010 bin

    -> the result of the & operation is 0x2
    and this is not 0, so its True

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    I'd say it returns true.

    0x07 in binary is 111 and 0x02 is 010, so when it does the math
    Code:
      111
    & 010
    ----------
      010
    which gives 2 as a result anyway.

    EDIT: Damn you IceBall, you beat me!
    Last edited by -=SoKrA=-; 12-28-2004 at 04:47 AM.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by DeeAitch
    How does an IF statement react on bitwise commands like this:
    Code:
       a = 0x07;
       b = 0x02;
    
       if (a & b)
          DoSomething();
    Is the IF statement TRUE or FALSE?
    There is no 'IF' in C. Maybe you meant 'if'.

    There is no 'command' in C, but statements, operators, expressions, functions...

    A C expression returns 0 or 1, not TRUE/FALSE.

    Simple question of boolean logic. What is the AND truth table, what is the result of 0x07 AND 0x02 ?
    Code:
          a = 0000 0111
          b = 0000 0010
    a AND b = ???? ????
    Shake your brain, it helps...
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21