How do Bitwise Operators work?

Hi everybody

I know a little bit of bitwise operators and I am getting some problems working with them.

For example, imagine the byte 0x42 (Or 01000010). Now I want to build a long having this byte on the first position starting from the left so I try the following code:

long l = 0x42 << 56;

However, this operation seems to work like if I have written:

long l = 0x42 << 24

That is, the new long is a String of only 32 bits. Why does this command ignore the first 32 bits? Any ideas?

[605 byte] By [astu18a] at [2007-11-27 11:50:17]
# 1

long l = 0x42L << 56;

BigDaddyLoveHandlesa at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 2

It works! Thanks a lot!

astu18a at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 3

> It works!

Do you understand why?

~

yawmarka at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 4

Not at all. Can you explain it?

astu18a at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 5

The reason, by the way, is that the expression 0x42 << 56 has type int --

its assignment to a long variable doesn't influence an expression's type.

BigDaddyLoveHandlesa at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok. Now I understand it. Thank you very much for your help.

astu18a at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 7

On a small tangent, I asked this question yesterday: what does this program output?

Guess, then copy and run to see if you were right.

public class PartyPretzel {

public static void main(String[] args) {

System.out.println(12345 + 5432l);

}

}

BigDaddyLoveHandlesa at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 8

> On a small tangent, I asked this question yesterday:

> what does this program output?

Grooooooooovy.

~

yawmarka at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 9

> On a small tangent, I asked this question yesterday:

> what does this program output?

> Guess, then copy and run to see if you were right.

> > public class PartyPretzel {

>public static void main(String[] args) {

>System.out.println(12345 + 5432l);

> }

> }

>

Wauu. I would have bet my hand saying that the result will be 66666 and it is 17777. And the reason for this is....

astu18a at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 10

> > On a small tangent, I asked this question

> yesterday:

> > what does this program output?

>

> Grooooooooovy.

A haunted JVM?

BigDaddyLoveHandlesa at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 11

> And the reason for this is....

Lack of attention to detail? ;o)

@BDLH: How do you get the code tags not to screw that up?

~

yawmarka at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 12

I would say because the numbers are greater than the maximum integer value. Am I right?

astu18a at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 13

Or at least one of them

astu18a at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 14

> I would say because the numbers are greater than the

> maximum integer value. Am I right?

Nope. 66666 is not that big, is it? Integer.MAX_VALUE is 2147483647

BigDaddyLoveHandlesa at 2007-7-29 18:30:26 > top of Java-index,Java Essentials,Java Programming...
# 15

> I would say because the numbers are greater than the

> maximum integer value. Am I right?

No. Not by a long shot.

Hint: It relates to the original problem in this thread.

jverda at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 16

> I would say because the numbers are greater than the

> maximum integer value. Am I right?

Good guess, but not correct.

1 != l

~

yawmarka at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 17

> @BDLH: How do you get the code tags not to screw that up?

I'll tell you when the cat is out of the bag.

BigDaddyLoveHandlesa at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 18

> I'll tell you when the cat is out of the bag.

I may have opened the bag... o_O

~

yawmarka at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 19

> @BDLH: How do you get the code tags not to screw that

> up?

Double it up, not?

jverda at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 20

> > @BDLH: How do you get the code tags not to screw

> that up?

>

> I'll tell you when the cat is out of the bag.

Okay: that stupid filter strips off the suffix if you write 5432l, so you have to write 5432ll

BigDaddyLoveHandlesa at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 21

Ok. Then maybe the numbers 12345 and 54321 are not written in a decimal scale?

astu18a at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 22

Come on guys. What's the reason for the strange output? I'm dying to know...

Guest123a at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 23

> Ok. Then maybe the numbers 12345 and 54321 are not

> written in a decimal scale?

The number 54321 is not present in that code.

Both numbers are decimal.

jverda at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 24

Copy paste the code.

Change the println and get rid of the "12345 + " part.

Run it.

System.out.println(5432l);

jverda at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 25

XD

Ok. I see. Good joke guys. I will use it with my partners!

astu18a at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 26

> > long l = 0x42 << 56;

>

l is a really bad variable name, by the way. :-)

jverda at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 27

Credits: I stole that example from Joshua Bloch and Neal Gafter, authors of the book Java Puzzlers.

Check out their Vodcast (May 18, 2007)

http://java.sun.com/developer/media/

In the discussion, they admit people hiss and groan with this particular puzzle.

BigDaddyLoveHandlesa at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 28

I really thought it was a Java problem/trick as oppose to mistyping problem. Not funny........waste of time!!

Guest123a at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 29

> l is a really bad variable name, by the way. :-)

I know. It was just for the example. I never use those names for the variables.

astu18a at 2007-7-29 18:30:31 > top of Java-index,Java Essentials,Java Programming...
# 30

> l is a really bad variable name, by the way. :-)

It's a really good variable name. Rule #22:

http://mindprod.com/jgloss/unmainnaming.html

~

yawmarka at 2007-7-29 18:30:36 > top of Java-index,Java Essentials,Java Programming...
# 31

> In the discussion, they admit people hiss and groan

> with this particular puzzle.

Reply #28. :o)

~

yawmarka at 2007-7-29 18:30:36 > top of Java-index,Java Essentials,Java Programming...