LuminaryJanitor
2[H]4U
- 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)?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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.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 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.LordBritish said:If all you wanna do is store a boolean value just use boolean or Boolean.
LuminaryJanitor is trying to figure out how to create a numeric binary literal in Java. You've shown him an article on bitwise operations.Pyrolistical said:Sure you can. Check out the bit wise shift operators and masking.
byte b = 0xffba;
int value = Integer.parseInt(s, 2);
Hex bitwise isn't bitwise. it's nibble-wise.svet-am said:can't do binary bitwise in Java (that I'm aware of), but you CAN do hex 'bitwise':
You could, but then you aren't using a literal, and you're slowing things down at compile-time.svet-am said:if you really MUST use binary, you can use this:
where the 2 tells it that you're parsing in base-2Code:int value = Integer.parseInt(s, 2);
svet-am said:can't do binary bitwise in Java (that I'm aware of), but you CAN do hex 'bitwise':
for example.Code:byte b = 0xffba;
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.drizzt81 said:Excuse my ignorance, but would that command try to fit 16 bits (4 nibbles) into a byte ... ?
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.mikeblas said:You could, but then you aren't using a literal, and you're slowing things down at compile-time.
Sorry; I meant execution time.LuminaryJanitor said:Are you sure it'd be executed at compile-time?