Bit Manipulation in Java

L

leslie_tighe

Hello,

I am new to working with the bit operators in java and need to
implement an algorightm and was hoping to get some guidance to address
the questions or url to material that may help.

1) I am starting out a byte array of lenght 20.
2) Next I need to determin the offsetbits, which are supposed to to be
the low order 4 bits of byte[19]. How do I determine this?
3)How do I convert this value in to an Int ?


Thanks in advance for your help.

Leslie
 
A

Andrey Kuznetsov

1) I am starting out a byte array of lenght 20.

byte [] b = new byte[20];
2) Next I need to determin the offsetbits, which are supposed to to be
the low order 4 bits of byte[19].

int mask = 15;

public int getOffsetBits() {
return b[19] & mask;
}
3)How do I convert this value in to an Int ?
don't know such type.
May be you want int or Integer?
 
A

Alan Krueger

Andrey said:
2) Next I need to determin the offsetbits, which are supposed to to be
the low order 4 bits of byte[19].

int mask = 15;

public int getOffsetBits() {
return b[19] & mask;
}

To tie in another thread, this may be one of those instances where
symbolic constants might be more maintainable than magic values.
 
Z

zero


Not really related to the OP, but I've always wondered if bit manipulations
in Java are really performant? In (relatively low to the ground) languages
like C and C++ manipulating individual bits can be done for reasons of
memory usage and/or speed. But does that apply for Java too, or does the
JVM handle bit manipulations in a way that levels out these advantages?
And if you're going to use bits, is it better to use a BitSet, or just use
raw data types and manipulate those bits? Anyone have some insight in
this?
 
R

Roedy Green

Not really related to the OP, but I've always wondered if bit manipulations
in Java are really performant? In (relatively low to the ground) languages
like C and C++ manipulating individual bits can be done for reasons of
memory usage and/or speed. But does that apply for Java too, or does the
JVM handle bit manipulations in a way that levels out these advantages?
And if you're going to use bits, is it better to use a BitSet, or just use
raw data types and manipulate those bits? Anyone have some insight in
this?

The JVM has bit operators that map very directly onto the bit
manipulation operators of most modern CPUs, so I would expect longs
and ints to do equally well to C with Hotspot or AOT compilers.

BitSet is for sets larger than 64 bits. It does the housekeeping to
simulate long bit strings with an array of longs.

see http://mindprod.com/jgloss/binary.html#BITSET
 
Z

zero

The JVM has bit operators that map very directly onto the bit
manipulation operators of most modern CPUs, so I would expect longs
and ints to do equally well to C with Hotspot or AOT compilers.

BitSet is for sets larger than 64 bits. It does the housekeeping to
simulate long bit strings with an array of longs.

see http://mindprod.com/jgloss/binary.html#BITSET

That's a very clear and helpful answer, thanks Roedy :)
 
J

John C. Bollinger

Alan said:
Andrey said:
2) Next I need to determin the offsetbits, which are supposed to to be
the low order 4 bits of byte[19].


int mask = 15;

public int getOffsetBits() {
return b[19] & mask;
}


To tie in another thread, this may be one of those instances where
symbolic constants might be more maintainable than magic values.

Agreed. Furthermore, I tend to use hexadecimal notation for literals
intended for this sort of purpose. It emphasizes (to me at least) that
it is not the value of the number that is important /per se/, but rather
its binary representation.
 
L

Luc The Perverse

To think that Sun would deliberately pass up a possible speed advantage by
choosing some form of integer arithmatic over binary ops, is ludicrous.

Now yes, if there is some hardware platform out there which does not contain
native implementations for these calls (I don't know of any) then the JVM
would have to work around that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top