bit manuplation with java

A

ali4ever4

well hi ther i have a strange question about bitmanuplation


if i have a set of bits such as 10110011


which can be any thing arrays of 1 and 0 or may be string or mostly
BitSet


is there is away to convert it to byte or may be to any other premitive
type such as int or char with the rest of the bits be zeros ??!!

for example if i have this stream of bits 00101 can i convert it to
integer with this bit representation
00000000 00000000 00000000 00000101
which equals to 5
hope i will find an answer to my question
and thanks very much in advance
 
V

VisionSet

well hi ther i have a strange question about bitmanuplation


if i have a set of bits such as 10110011


which can be any thing arrays of 1 and 0 or may be string or mostly
BitSet


is there is away to convert it to byte or may be to any other premitive
type such as int or char with the rest of the bits be zeros ??!!

for example if i have this stream of bits 00101 can i convert it to
integer with this bit representation
00000000 00000000 00000000 00000101
which equals to 5
hope i will find an answer to my question
and thanks very much in advance

if you know it will fit into int range then read the bytes (which presumably
have no signed meaning) do an OR with an int and shift the int 8 places left

ie

int i = 0;
byte[4] b; // assigned from stream
for(j = 0; j < b.length; j++) {
i << 8
i = i | b[j]
}

maybe there is an easier way
 
A

ali

well thanks alot VisionSet you saved me from a lot of things by your
replay thanks a lot
 
R

Ryan Stewart

well hi ther i have a strange question about bitmanuplation
if i have a set of bits such as 10110011

which can be any thing arrays of 1 and 0 or may be string or mostly
BitSet

is there is away to convert it to byte or may be to any other premitive
type such as int or char with the rest of the bits be zeros ??!!

for example if i have this stream of bits 00101 can i convert it to
integer with this bit representation
00000000 00000000 00000000 00000101
which equals to 5
hope i will find an answer to my question
and thanks very much in advance
You've already had answers to your question the last time you asked it.
 
A

ali

ohh sorry yesterday i was searching for a group where i can ask this
question and i found yours and put my question but because i am new to
googlegroups i didnt knew how to return to it today and i didnt added
it to my favorites so i searched again for groups today and found this
one again and couldnt remmember that it is the same group where i
posted my question so i posted it again and now i found my old post
well thanks alot for who posted the answer there too i really loved
your group

and sorry again for the multi-post
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top