Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shifting

 
Frank Jacobsen
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic