• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to represent 32 bit unsigned integer array in java  RSS feed

 
Radha Gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question in representing numbers.
I have a array of integers. The integers are represented as 32 bit unsigned and I would want to do bit manipulation on the 32 bits (either set or reset). How can I represent this in java?

Please correct me if i am wrong ..

If I say it as long[] A , then when I convert an array element into byte, I would want 32 bits . But I would get it as 8 bits only (byteValue() funtion returns just a converted byte() )

Should I represent it as an list containing 32 bit array each? I am just confused of the efficient representation.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain why you cannot do the bit manipulation on two’s complement numbers. What exactly are you doing?
 
Radha Gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. I am just a newbie .Please excuse me if i am wrong.


For example : I have an array of 100 elements. For each element,there are 32 bit(unsigned).Assume I want to set the 30th bit to 1 for some elements.

How can I represent this efficiently in java?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radha Gopal wrote:Thanks for your response. I am just a newbie .Please excuse me if i am wrong.


For example : I have an array of 100 elements. For each element,there are 32 bit(unsigned).Assume I want to set the 30th bit to 1 for some elements.

How can I represent this efficiently in java?



In java, int types are 32 bit signed. And if all you care to do is to do bit manipulation (not math operations), it doesn't matter if the type is signed or unsigned.

Henry
 
Radha Gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case if I want to retrieve the 30 th bit , how can i do that in java?My thought was to convert into a binarystring and retreive the appropraite character. Is this corerct?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perform a bitshift and apply a bitmask to the result?
 
Radha Gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just came acroos a util BitSet which just performs the function that I needed. thanks..
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radha Gopal wrote:Should I represent it as an list containing 32 bit array each? I am just confused of the efficient representation.

As usual with something like this, everything depends on how you intend to use them.
1. There is no such thing as an unsigned int in Java.
2. Any Java int can be converted to an unsigned long with
long x = int & 0xFFFFFFFFL;
but it does require time (not much; but if many millions are involved, possibly significant).
3. An array: No. Java array indexes must be an int >= 0.

It might be better to explain exactly what you want. I have an idea for a solution; but from what you've supplied, it's difficult to know whether I'm right.

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will by now know that you can get the state of the 30th bit withYou can do the same for the most significant bit, using 31 instead of 30.

I am using the convention that the least significant bit is number 0 and bit numbers count up towards the left to 31 for an int.
I think the () around 1 << 30 are redundant because the shift operators have a higher precedence than &, but the () round & are necessary because != has a higher precedence.
The bitwise operators including << and & can be executed very rapidly, and so can ==/!=.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!