Bit patterns in Java

Joined
Oct 26, 2005
Messages
2,340
Is there any way to directly enter a binary number in Java (i.e. something analogous to the "0x" for hex numbers)?
 
If all you wanna do is store a boolean value just use boolean or Boolean.

Don't worry about wasted memory space. It may not be an actual bit underneath but with gigs of memory who cares?

Back in the old days memory was expensive so everfy bit counted.
 
LordBritish said:
If all you wanna do is store a boolean value just use boolean or Boolean.

Don't worry about wasted memory space. It may not be an actual bit underneath but with gigs of memory who cares?

Back in the old days memory was expensive so everfy bit counted.
it may also be that bit-wise operations are more expensive (or equivalently expensive) as byte operations. I cannot say for sure, since my knowledge of current generation CPUs is rather limited.
 
Nah, what I meant is something equivalent to the prefixes 0x for hex and 0 for octal, so I can enter binary numbers as "10101001" rather than having to convert them myself. I don't need it any more, but it'd be good to know anyway.

LordBritish said:
If all you wanna do is store a boolean value just use boolean or Boolean.
It was for a MIPS disassembler. I was trying to compare two instructions with some of the fields omitted, so I wanted to use (instruction) & 11111100000111... to remove fields.
 
oh. DUH

I don't know if it is possible, but looking here which has an article taking about bitmasks etc up and down, yet uses decimal notation for input...

then again, you may want to "try" this format:

0b01111110
 
Pyrolistical said:
Sure you can. Check out the bit wise shift operators and masking.
LuminaryJanitor is trying to figure out how to create a numeric binary literal in Java. You've shown him an article on bitwise operations.

Curiously, while C (and C++) and Java provide octal and hex, they don't provide a prefix to indicate that a literal is binary.
 
can't do binary bitwise in Java (that I'm aware of), but you CAN do hex 'bitwise':

Code:
byte b = 0xffba;
for example.

if you really MUST use binary, you can use this:

Code:
 int value = Integer.parseInt(s, 2);
where the 2 tells it that you're parsing in base-2

here's a link to an introduction into making 'programmer parsable' binary strings
http://javaalmanac.com/egs/java.math/Bytes2Str.html

The link below will introduce you to the Java bitwise operators.
http://leepoint.net/notes-java/data/expressions/bitops.html
 
svet-am said:
can't do binary bitwise in Java (that I'm aware of), but you CAN do hex 'bitwise':
Hex bitwise isn't bitwise. it's nibble-wise.
svet-am said:
if you really MUST use binary, you can use this:

Code:
 int value = Integer.parseInt(s, 2);
where the 2 tells it that you're parsing in base-2
You could, but then you aren't using a literal, and you're slowing things down at compile-time.
 
svet-am said:
can't do binary bitwise in Java (that I'm aware of), but you CAN do hex 'bitwise':

Code:
byte b = 0xffba;
for example.

Excuse my ignorance, but would that command try to fit 16 bits (4 nibbles) into a byte, which I consider to be 8 bits in many cases?
 
drizzt81 said:
Excuse my ignorance, but would that command try to fit 16 bits (4 nibbles) into a byte ... ?
Yes. Actually, it'd try to fit 32 bits into 8, since 0xffba is treated as an int, and wouldn't even compile. An explicit cast to byte truncates it to 0xba.

mikeblas said:
You could, but then you aren't using a literal, and you're slowing things down at compile-time.
Yeah, the point was really just to save me opening up the calculator to do the hex conversion myself (plus the bit pattern makes it a little more readable than the hex), so any impact on the program itself isn't really worth it.

EDIT: Are you sure it'd be executed at compile-time? I'm pretty sure a static method could be bound at compile time (unlike methods called on object references), but to evaluate the return at compile-time, the compiler would need to be able to ensure complete referential transparency (i.e. no dependence on, or modification to, the runtime environment).
 
According to the Java Language Specification, there isn't a way to write binary numbers directly into the code. Decimal, hex, and octal are the three supported formats. F'n annoying, but that's how it is.

You could do something like
static int b11111100000111 = Integer.parseInt("11111100000111", 2);
to avoid parsing the number more than once if you really wanted to use binary.

Java doesn't have a preprocessor like C/C++, so there isn't any way to do a binary as string->int conversion at compile time. You could write a preprocessor of sorts of your own to convert some binary format to hex & run your code through it every time you compile, but that seems like a bit of overkill for what's required here.

I'd probably just convert to hex & put a comment next to it describing the bit pattern.
 
Back
Top