Thread: bit testing question

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    bit testing question

    I don't think there is, but is there any difference between the following expressions?

    Code:
    ...
    unsigned char flag ; 
    
    #define BIT0 0x80  
    #define BIT1 0x40  
    ...
    
    if (flag & BIT0) ...    ; 
    
    if ((flag & BIT0) == BIT0) ... ; 
    ...
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Dino View Post
    I don't think there is, but is there any difference between the following expressions?

    Code:
    ...
    unsigned char flag ; 
    
    #define BIT0 0x80  
    #define BIT1 0x40  
    ...
    
    if (flag & BIT0) ...    ; 
    
    if ((flag & BIT0) == BIT0) ... ; 
    ...
    First, flag is uninitialized, so we cannot answer your question.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I appreciate your replying, but IMHO, the pertinent pieces of the question were more than enough to answer. The question was about syntax, style and expression evaluation.

    However, I have tested this myself and the result is both flavors of expressions yield the same result, (regardless of how flag is set) with the latter == comparison in the second example being redundant and unnecessary.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    They're the same thing, so long as you're only testing one bit at a time.

    If you're testing multiple bits, the former will be true if any bits match, whereas the latter will only be true if all bits match.


    > with the latter == comparison in the second example being redundant and unnecessary.
    I guess it depends on how you view redundant.

    Sure, it's half a second of typing, a few bytes extra of disk space, and unmeasurable amounts of nanoseconds extra on the compilation.

    For the human reader, the latter is rather more obvious than the former.
    Even more so if the bits had names.
    Code:
    #define SWITCH_ON_BIT 0x80  
    #define MOTOR_ON_BIT 0x40  
    ...
    if ((flag & SWITCH_ON_BIT) == SWITCH_ON_BIT)

    Any decent compiler will generate the same code in either case (as gcc does), so there isn't a "for performance" argument to be made.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    There are a couple of differences. First, as @Salem points out, is the fact that the sameness only holds when the mask you are testing is a single bit. If your mask includes 2 or more bits, and your input only matches some of them, then the flag & MASK case will be non-zero (true) when the equality case is false.

    This means if your question was really "what coding style should I prefer when writing this kind of test", you should re-think.

    Second, the result of the expression is different. This doesn't matter in this specific example, because the result is being collapsed into zero/nonzero by the surrounding if (), but in other cases your flag & BIT0 will have a value of 0x80, while (flag & BIT0) == BIT0 will have a value of 1. (Or zero, of course, but that's the same in both flavors.)

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thank you both for replying. I would have responded earlier, but couldn't - see below.

    I went through several test cases to see what is happening and to understand it better. I guess it's pretty straight forward once you study it.

    Code:
    #define BIT0 0x80
    #define BIT4 0x08  
    unsigned char flag ; 
    
    0x88 : if  (flag & BIT0)        is true                     
    0x88 : if ((flag & BIT0)==0)    is false                    
    0x88 : if ((flag & BIT0)==BIT0) is true                     
                                                                
    0x88 : if  (flag & (BIT0+BIT4))             is true         
    0x88 : if ((flag & (BIT0+BIT4))==0)         is false        
    0x88 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is true       
                                                                
    0x80 : if  (flag & BIT0)        is true                     
    0x80 : if ((flag & BIT0)==0)    is false                    
    0x80 : if ((flag & BIT0)==BIT0) is true                     
                                                                
    0x80 : if  (flag & (BIT0+BIT4))             is true         
    0x80 : if ((flag & (BIT0+BIT4))==0)         is false        
    0x80 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is false      
                                                                
    0x08 : if  (flag & BIT0)        is false                    
    0x08 : if ((flag & BIT0)==0)    is true                     
    0x08 : if ((flag & BIT0)==BIT0) is false                    
                                                                
    0x08 : if  (flag & (BIT0+BIT4))             is true         
    0x08 : if ((flag & (BIT0+BIT4))==0)         is false        
    0x08 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is false      
                                                                
    0x00 : if  (flag & BIT0)        is false                    
    0x00 : if ((flag & BIT0)==0)    is true                     
    0x00 : if ((flag & BIT0)==BIT0) is false                    
                                                                
    0x00 : if  (flag & (BIT0+BIT4))             is false        
    0x00 : if ((flag & (BIT0+BIT4))==0)         is true         
    0x00 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is false
    On another note... I'm having issues with this site using Chrome. There are 2 things that are occurring often.

    First, I get an error dialog (that I am typing in now!) that tells me my post was too short and needs to be at least 4 characters. I just tried post all the above remarks and the code inside the code tags, so I'm certainly meeting that criteria.

    Second, when I come back to this site to read and check for replies, I am unable to type text in the Quick Reply box. There box appears to not be fully loaded. I'm attaching a screen shot of both issues.

    Are these known issues? Is it just a Chrome thing? I'm up to date w/Chrome. Running Win 10.

    bit testing question-c-programming-quick-reply-jpgbit testing question-c-programming-4-chars-png
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Regarding the Chrome issue.

    Force reload the page to ignore your local cache (How to force refresh without cache in Google Chrome? - Super User)

    Clean the cache and cookies.

    Disable any ad/script blockers (post length and code tags are client side bits of Javascript).

    Try incognito mode.

    Try creating a new empty profile in Chrome.

    Try another browser on the same machine.

    > #define BIT0 0x80
    Most people would pick bit 0 to mean the LSB.
    For example, it wouldn't change it's meaning if you changed from 8 bits to 16 bits.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function testing question
    By towed in forum C Programming
    Replies: 3
    Last Post: 12-13-2010, 10:23 AM
  2. Class testing question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2009, 05:41 AM
  3. Hi everyone..Testing strings and if question
    By tinkerbell20 in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2005, 06:23 AM
  4. Condition testing question
    By Aerie in forum C Programming
    Replies: 21
    Last Post: 12-13-2004, 06:54 PM
  5. Question about Software Testing
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-08-2002, 09:47 AM

Tags for this Thread