Java Bit Manipulation

  • Thread starter Gilbert Ostlethwaite
  • Start date
G

Gilbert Ostlethwaite

Hi

I need to bit-twiddle using a = b & mask where a and b are integers,
but mask holds my masking information eg 0xFF as a string. Obviously
this isn't working, but I can't find how to convert my string
containing 0xFF into an integer - Integer.parseInt(mask) throws a
NumberFormatException.

Regards
 
A

Andreas Leitgeb

Gilbert Ostlethwaite said:
I need to bit-twiddle using a = b & mask where a and b are integers,
but mask holds my masking information eg 0xFF as a string. Obviously
this isn't working, but I can't find how to convert my string
containing 0xFF into an integer - Integer.parseInt(mask) throws a
NumberFormatException.

You'll probably have to deal with the "0x" yourself, i.e., see if
the string starts with "0x", and of so then do
Integer.parseInt( maskString.substring(2) , 16 )
instead (second argument 16 is the radix).

You could then also accept "0b" for binary, which is much less
common, but perhaps convenient to whoever has to provide the
mask string.
 
A

Andreas Leitgeb

bugbear said:
Just to preempt, java has no unsigned integer types,
so be careful.

While signedness of a bit-mask is rather void, the theoretical
signedness of the input may be a problem: FFFFFFFF, or even
80000000 parsed with a radix of 16 will cause NumberFormatExceptions
in Integer.parseInt().

To avoid it, rather use Long.parseLong(maskString.substring(2),16)
(".substring(2)" only for strings that really start with "0x"),
and then eventually cast back to int.

If you're sure, that your mask never uses more than 31 bits, then
you can forget about the "Long" and use Integer.parseInt(...).
 
R

Roedy Green

I need to bit-twiddle using a = b & mask where a and b are integers,
but mask holds my masking information eg 0xFF as a string. Obviously
this isn't working, but I can't find how to convert my string
containing 0xFF into an integer - Integer.parseInt(mask) throws a
NumberFormatException.

see http://mindprod.com/applet/converter.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"By 2040, the Sahara will be moving into Europe, and Berlin
will be as hot as Baghdad. Atlanta will end up a kudzu
jungle. Phoenix will become uninhabitable, as will parts of
Beijing (desert), Miami (rising seas) and London (floods).
Food shortages will drive millions of people north, raising
political tensions."
~ James Lovelock
Lovelock is more pessimistic than the consensus, because he thinks man will
refuse to take significant action to ameliorate global warming.
 
T

Thomas Schodt

Gilbert said:
I need to bit-twiddle using a = b & mask where a and b are integers,
but mask holds my masking information eg 0xFF as a string. Obviously
this isn't working, but I can't find how to convert my string
containing 0xFF into an integer - Integer.parseInt(mask) throws a
NumberFormatException.
Integer.decode(mask)
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top