Java Bit-Level Programming

Hi,

I am working on a file which has these chracteristics.

File has many frames and each frame has certain no of bytes.

a) One byte of a file is defined as

frame_type (4 bits) Value = 0100b

error_detection_type (2 bits)Value = 00b

SOF (1 bit)Value = 1b

EOF (1 bit)Value = 0b

I can read bytes of a file in an array, but how to read bit within the byte.

Ex: After reading the bytes in a array how do to verify the bit inside the byte (say EOF=0b or SOF=1b).

[518 byte] By [Santosh.Bhushana] at [2007-11-27 4:55:32]
# 1
By using [url= http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22]bitwise operations[/url].
stefan.schulza at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 2
Hi,for some typical operations You can use Integer, for instance http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#toBinaryString(int).
_Dima_a at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 3
> Hi,> > for some typical operations You can use Integer, ...No, that will just return a String representation of an integer value which is not suitable for bitwise operations.@OP: better look at the link posted in reply #1.
prometheuzza at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 4
2prometheuzzAs I can see there is no need to change something only to read. So, Your comment looks unrelevant.Regards.
_Dima_a at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 5

> 2prometheuzz

>

> As I can see there is no need to change something

> only to read. So, Your comment looks unrelevant.

>

> Regards.

In the original post the OP mentioned that a file consist of many frames and each frame consists of several bytes.

So, you suggest converting each byte to a binary String representation and then checking that String if a certain bit is present in some position? That would mean using an awfull lot of charAt(...) or substring(...) methods on a lot of String objects: not very efficient...

@OP: better use bitwise and bit shift operators.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html

prometheuzza at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 6

Thanks to all of you.

Yes i can see toBinaryString(int i) in the Integer class which i can use to convert into a binary string and if i want to to get the bit value i can parse the string(is there any other way to read the bit value ex; 3rd bit within the byte. )

My duke points to Dima.

Santosh.Bhushana at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 7
Great, take the most inept solution and be happy.
stefan.schulza at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 8

> ....

> (is there any other way to read the bit value

> ex; 3rd bit within the byte. )

> ...

Yes: as already mentioned in the first reply: by using some simple bit shifting operations!

Example:

byte theByte = 92;

// shift all bits two places to the right (>> operator), and AND (& operator) the result with the number 1

int _3rdBit = (theByte >> 2) & 1;

System.out.println("The third bit of "+theByte+" ("+

Integer.toBinaryString(theByte)+" in binary) = "+_3rdBit);

prometheuzza at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 9
// 2 ^ 0 -> 1st Bit, 2 ^ 1 -> 2nd Bit, 2 ^ 2 -> 3rd Bitint _3rdBit = theByte & 4;Of course, _3rdBit here would be 4 not 1 if set.
stefan.schulza at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 10
Yes, i got the concept now. Thank you stefan(you r right, this is a better way) and prometheuzz !!!
Santosh.Bhushana at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...
# 11
Yes, i got the concept now.Thank you stefan(you r right, this is a better way) and prometheuzz !!!
Santosh.Bhushana at 2007-7-12 10:10:30 > top of Java-index,Core,Core APIs...