• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to represent 32 bit unsigned integer array in java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ==/!=.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic