How to set and read individual bits from a byte using java

Posted on 2008-11-17
Last Modified: 2013-12-22
I have a requirement to define a variable as byte in java. And I need to set and access individual bits from the byte to do specific operations like (example) if first bit is 1 then some action and so on.
Question by:kishoretnn
    LVL 24

    Accepted Solution

    1) use int.

    2) set:    int k;   k |= 1;
       or         k |= 1<<5;   //  (00100000)

    3) read:   int m =  k & (1<<3);

    although, boolean result is better:

    boolean h = (k & (1<<3)) != 0;  // 1 == true.

    LVL 86

    Assisted Solution

    You could always turn it into a BitSet then it could be a bit easier for you
    LVL 6

    Expert Comment

    You can use the the following bitwise operators (some examples given from the experts above):

    Bitwise And Operation:
    k = 2 & 1;    // result 0    
    k = 2 & 2;   //result 2
    k = 3 & 1; //result 1

    Example of k=3&1    (Basically you compare each bit and if both bit places are same keep them)

    Bitwise Or Operation:
    k = 2 | 1;    // result 3    
    k = 2 | 2;   //result 2
    k = 3 | 1; //result 3


    Example of k=3&1    (Keep all bits with 1s)

    | 00000001

    Bitwise Shift operators >>, <<

    k = 1 << 1  // result k = 2
    k = 1 << 2  //result k = 4
    k = 4 >> 1 //result k = 2
    k = 4 >> 2 // result k = 1

    Here the << operator shifts all bits to the Right (increasing), and the >> operator shifts all bits to the left (decreasing)
    LVL 6

    Expert Comment

    One more note:

    the bitwise shift operators:

    >>>, and <<< are exactly the same as the >>, << operators, expect that they have the extra >, <
    LVL 1

    Expert Comment

    To RishadanPort: Not exactly!
    First of all <<< does not exist. Second, >> is right shift with sign extension and >>> is right shift with zero extension.
    LVL 6

    Expert Comment

    interesting. I think I was thinking of C++ or C
    LVL 24

    Expert Comment

    The BitSet that CEHJ mentioned is a universal solution.
    the operators | & << >> and >>> are much faster when applied
    on an int, but restricted  to 32 bits.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now