Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] How do I do bit-shifting in Java?

Started by EGD Eric Dec 11, 2005 at 6:29 PM 11 replies 5.6k views
Original Post
EGD Eric
EGD Eric
I always get compiler errors when I use << or >>, so I'm guessing it must be different in Java than in C.
DrjonesDW3d
DrjonesDW3d
Quote:
Original post by EGD Eric
I always get compiler errors when I use << or >>, so I'm guessing it must be different in Java than in C.


What kind of errors do you get?

Working with bits in java is a pain. I did some data compression stuff a while back where you were looking at everything in the bit level, it starts to become a real hassle.

One of the major problems is that in Java a "byte" is signed. That's right, something that is almost exclusivly used for working with bits, where the value doesn't always represent something numerical is signed. That creates a lot of strange errors when shifting and working at the bit level. One thing that helped me, was that if I would cast a byte as a byte, some of those problems would go away. Yes, it doesn't make sense, but it works :)
bilsa
bilsa
Bitshifting works much the same as in c++:

int val = 1;
val = (val << 3);
val <<= 3;
val = (val >> 3);
val >>= 3;
val = (val >>> 3);
val >>>= 3;

Darragh
Darragh
Quote:
Original post by DrjonesDW3d
Working with bits in java is a pain. I did some data compression stuff a while back where you were looking at everything in the bit level, it starts to become a real hassle.


Not really. The only thing that java is a bit fussy about is type safety. You'll have to do a few typecasts in order to reassure the compilier that you know what you are doing. Other than that its pretty much similar to C/C++

Quote:
One of the major problems is that in Java a "byte" is signed. That's right, something that is almost exclusivly used for working with bits, where the value doesn't always represent something numerical is signed. That creates a lot of strange errors when shifting and working at the bit level. One thing that helped me, was that if I would cast a byte as a byte, some of those problems would go away. Yes, it doesn't make sense, but it works :)


I haven't had any problems with this. Having everything signed only affects how the data is interpreted, not the actual value of the bits. Eg: 0x80 is 128 in unsigned bytes, and -128 in signed bytes, but its still exactly the same data.

EGD Eric
EGD Eric
Now I realise what I was doing wrong, I was trying something like this:

val << 8;

that doesn't work, you have to go: val = val << 8;
I had even looked at the very same page that visage provided a link to, but that's what made me think I was supposed to do this val << 8 crap.
Thanks, now I know!
Iftah
Iftah
there is a big problem with java bit shifting,
there is no support for unsigned int (!!!)

so shifting keeps the sign of the number!

for example:

int a = 0x80000000;
a = a >> 1; ---> a== 0xC0000000 not 0x40000000 !!!

for my AI homework i wanted to compact memory as much as possible (making 64 bits is much smaller than array of 64 chars) and it made perfect sense since the puzzle i had to solve had rotating rings in it so bit shifting made rotating easy, but the sign problem made me switch to C++ (where there is unsigned without hassle).

Iftah.
doynax
doynax
Quote:
Original post by Iftah
there is a big problem with java bit shifting,
there is no support for unsigned int (!!!)

so shifting keeps the sign of the number!
That's why Java has a special logical right shift operator (>>>) in addition to the arithmetic one (>>).
EGD Eric
EGD Eric
What's the difference between the arithmetic and logical operators? One keeps the sign, and the other one doesn't?

for my AI homework i wanted to compact memory as much as possible...
You had AI homework? You were (are) taking some kind of 3-year intensive computer science course? Or a game programming course?
doynax
doynax
Quote:
Original post by EGD Eric
What's the difference between the arithmetic and logical operators? One keeps the sign, and the other one doesn't?
Exactly. The arithmetic one keeps the sign, so you can use it to simulate a division of signed integers (beware of rounding differences however).
Quote:
Original post by EGD Eric
for my AI homework i wanted to compact memory as much as possible...
You had AI homework? You were (are) taking some kind of 3-year intensive computer science course? Or a game programming course?
Doesn't most universities offer AI courses? I know my crappy local school does, without any special prerequisites.
I'm considering swapping one of my regular courses for one in AI next period actually.
Fred304
Fred304
Quote:
Original post by EGD Eric
Now I realise what I was doing wrong, I was trying something like this:

val << 8;

that doesn't work, you have to go: val = val << 8;

Mind that this is nothing special about the >> operator. For example:
int x = 5;x+1;

does not work either, you have to use x = x+1;
CaptainJester
CaptainJester
Quote:
Original post by Fred304
Quote:
Original post by EGD Eric
Now I realise what I was doing wrong, I was trying something like this:

val << 8;

that doesn't work, you have to go: val = val << 8;

Mind that this is nothing special about the >> operator. For example:
int x = 5;x+1;

does not work either, you have to use x = x+1;


I think you mean x += 1; which Java can do.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.