Solved

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

Posted on 2008-11-17
Medium Priority
1,937 Views
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.
0
Question by:kishoretnn

LVL 24

Accepted Solution

sciuriware earned 100 total points
ID: 22978784
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.

;JOOP!
0

LVL 86

Assisted Solution

CEHJ earned 100 total points
ID: 22978832
You could always turn it into a BitSet then it could be a bit easier for you
0

LVL 6

Expert Comment

ID: 22979307
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)
00000011
&00000001
------------
00000001

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

Example:

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

00000011
| 00000001
------------
00000011

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)

0

LVL 6

Expert Comment

ID: 22979338
One more note:

the bitwise shift operators:

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

LVL 1

Expert Comment

ID: 22979731
First of all <<< does not exist. Second, >> is right shift with sign extension and >>> is right shift with zero extension.

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121
0

LVL 6

Expert Comment

ID: 22979752
interesting. I think I was thinking of C++ or C
0

LVL 24

Expert Comment

ID: 22979994
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.

;JOOP!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.
###### Suggested Courses
Course of the Month16 days, 8 hours left to enroll