Validating bits

Posted on 2006-06-04
Last Modified: 2006-11-18
I have a char (1 byte) value inside a struct.  I need to check each bit - and if certain bits are set, perform certain operations.

How do i check each bit within a byte?  
Question by:jewee
    LVL 57

    Expert Comment

    What language?
    LVL 8

    Accepted Solution


    #include <iostream>

    using namespace std;

    int main()
          char T ='D';
          for (int i=0;i<8;i++)
                cout << (T & (1 << i)) << ' ';
          return 0;

    This shows you how you can use the bit operator to get the bit value from each.  You will see from that
    example that it outputs:

    0,0,4,0,0,0,64,0  Which tells you off/on for each if it is not 0.  The value is the value that bit represents.

    Author Comment

    Sorry, I meant to post this under the C programming language.
    LVL 8

    Expert Comment

    Should work for C as well.
    LVL 5

    Expert Comment

    I would suggest you define the meanings for your bits (hex is often easiest):

    #define VALID_USER 0x01
    #define LOGGED_IN 0x02
    #define HAS_DOG 0x04
    #define FAST_CAR 0x08
    #define GOLD_CARD 0x10

    You could then define a specific combination of bits you want to test for with the "or" operater:


    Then, loop the bytes you want to check, and test each byte with the "and" operater:

    if ((c & VALID_LOG_CAR) == VALID_LOG_CAR)


    Hope this helps.


    Author Comment

    I am looking to validate each one individually - for example, if bit 7 is set, then perform some operation, if bit 6, then call another function...etc.


    Author Comment

    I am reading from a binary file
    LVL 8

    Expert Comment

    Jewee that is what my example does, take a look.  You get the byte and then you can view each individual bit to see
    if it is set or not.  If it is 0 it is not set, if it is not 0 then it is set.  The value that you retrieve will be the value of that
    bits placeholder.  The bits run 0-7 for the byte, so you check each like so:

    (MyByte & (1 << 0)) //Check bit 0
    (MyByte & (1 << 0)) //Check bit 1
    (MyByte & (1 << 0)) //Check bit 2
    (MyByte & (1 << 0)) //Check bit 3
    (MyByte & (1 << 0)) //Check bit 4
    (MyByte & (1 << 0)) //Check bit 5
    (MyByte & (1 << 0)) //Check bit 6
    (MyByte & (1 << 0)) //Check bit 7

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now