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

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You could always turn it into a BitSet then it could be a bit easier for you
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)
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

One more note:

the bitwise shift operators:

>>>, and <<< are exactly the same as the >>, << operators, expect that they have the extra >, <
To RishadanPort: Not exactly!
First of all <<< does not exist. Second, >> is right shift with sign extension and >>> is right shift with zero extension.
interesting. I think I was thinking of C++ or C
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.