Thread: bit operation problem

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    bit operation problem

    I got a interview question previous like this:
    find what might be wrong

    if (mask & ASYNC_BIT == 0x0100) {....

    No values were defined for both "mask" and ASYNC_BIT in the question, just wonder in what condition the result of "AND" operation could not compare with hex value.


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Syntactically: == comes first, & comes second.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The order of precedence between those operations are "bad": you first do the ==, then do the and for some reason. You need parenthesis to make it right, e.g.
    Code:
    if ((mask & ASYNC_BIT) == 0x0100)
    Without parenthesis, it becomes:
    Code:
    if (mask & (ASYNC_BIT == 0x0100))
    which turns into an and with either 1 or 0.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    Oh that's really basic thing and I understand why i made a mistake then.
    thx

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I wouldn't have known that without looking at a precedence table. I'd tell the interviewers I can answer the question if they either show me a precedence chart or put some parentheses in.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pinklax View Post
    Oh that's really basic thing and I understand why i made a mistake then.
    thx
    Aside from cpjust's answer, I have made the mistake of writing code like that so many times that I'm now ACTIVELY thinking about it when I'm writing bit-checking/bit-twiddling code - a bit like someone who's flooded the bathroom 6 times will double and tripple-checkl that they haven't left the taps on.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. bit clearing problem
    By Buckshot in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2005, 07:36 PM
  5. binary numbers
    By watshamacalit in forum C Programming
    Replies: 4
    Last Post: 01-14-2003, 11:06 PM