aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shifting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shifting " Watch "Shifting " New topic
Author

Shifting

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
byte c = (byte) b << b2;
Why can�t this compile, if i replace the first this statement with:
int c = (byte) b << b2;
it compiles fine and give me the result 20.
Craig Arthur
Greenhorn

Joined: Dec 14, 2001
Posts: 5
Assuming b and b2 are bytes, unary numeric promotion is applied making them both ints. The result is also an int. Use parenthesis to do the cast like this:

- greg
[ June 20, 2002: Message edited by: greg millward ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is from the JLS
The type of each of the operands of a shift operator must be a primitive integral type, or a compile-time error occurs. Binary numeric promotion (�5.6.2) is not performed on the operands; rather, unary numeric promotion (�5.6.1) is per-formed on each operand separately. The type of the shift expression is the pro-moted type of the left-hand operand.
Which implies it would be applicable to int and long only
Hope this helps
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
You say that: The type of each of the operands of a shift operator must be a primitive integral type
Long or Int, should i understand it this way, that if i use the shift operator on a byte, char, short , float or double i need to use a cast or ???
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Integral type doens't mean it has to be an int. It just means that it can't be a decimal value. It must be an integer.
Corey


SCJP Tipline, etc.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Let me try:
given a << b, a and b can any of the integral primitives: byte, short, int or long. Individually, a and b will be "int-promoted" first (if applicable). The return value of the expression will be the "int-promoted" type of a, whatever that is.
For example:
byte << byte returns int
short << int returns int
int << long still returns int
long << int returns long
HTH
[ June 20, 2002: Message edited by: Anthony Villanueva ]
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
Once again i tried this:
char b = 8;
int b2 = 2;
int c = b << b2;
System.out.println("c: " + c);

this code compile fine !
int b = 8;
char b2 = 2;

int i = 2;

int c = b << b2;

System.out.println("c: " + c);
This also compile fine !!!
That must be the fact that char is promoted to int !!!
But infact if i use the same code with doubles the compiler says: VisualCafe (22:08:48): Error: C:\javacert\javacert.java(12): operator << cannot be applied to double,char
But the compiler lies, this can be applied to a char !!!
I think i understand bitshift now, but i still dont understand why the compiler, tell med that char can�t be shifted
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
A char is also considered a primitive integral (think of it as an unsigned int). Try this:

Anyway, you cannot use floats or doubles because you don't have a binary representation of either, nor can you bit-shift a quantity by a non-integral value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shifting