Home > Default > Left-shift operator used on byte values

Left-shift operator used on byte values

November 30Hits:0
Advertisement
Hello,
I'm reviewing some problems, and I need some help. I have a program that has some code like the following:
byte y = 10; // 00001010 in binary
byte result = (byte) (y << 1);
System.out.println("result: " + result); // 20.  Ok.
result = (byte) (y << 7);
System.out.println("result: " + result); // 0.  Ok.
result = (byte) (y << 8);
System.out.println("result: " + result); // 0. Why???
// I was expecting a shift of 0 bits because the
// right-hand operand is equal to the number of
// bits for the size of the result type--in this case
// 8 bits for a byte.
// 8 % 8 = 0 number of bits for the shift.
result = (byte) (y << 6);
System.out.println("result: " + result); // -128.  Ok.
result = (byte) (y << 10);
System.out.println("result: " + result); // 0.  Why???
// Shouldn't it be 2 bits for the shift?
// That is, 10 % 8 = 2.
// I was expecting 40 as the the answer for this one.I understand that for binary operations that the operands will be promoted to at least int types before execution occurs, but I still don't see how it would make a difference for the left-shift operator. Any help and clarification on this will be appreciated. It would be helpful to see the binary representation of the the "result" variable for the ones that I'm asking about. Thanks in advance.

Answers

result = (byte) (y << 8);
System.out.println("result: " + result); // 0. Why???
// I was expecting a shift of 0 bits because the
// right-hand operand is equal to the number of
// bits for the size of the result type--in this case
// 8 bits for a byte.the result of (y << 8) is an int, not a byte. the byte "y" is promoted to int for the bit shift. so the int result of the bit shift is 00000000 00000000 00001010 00000000. when you cast that back to byte, the 24 leftmost bits get lopped off, and you're left with zero.
hth,
p

Read other 5 answers

Tags:

Related Articles

  • Left-shift operator used on byte valuesNovember 30

    Hello, I'm reviewing some problems, and I need some help. I have a program that has some code like the following: byte y = 10; // 00001010 in binary byte result = (byte) (y << 1); System.out.println("result: " + result); // 20.  Ok. result

  • Logical right shift ( ) operatorNovember 30

    Hi, I suspect a bug in the logical right shift operator >>> (as opposed to the arithmetic right shift >>). The documentation says "0-s will be shifted from the left". It is not the case. byte b = -128;                         // t

  • JAVA (SHIFT OPERATOR)November 30

    Hi, I really want to know SHIFT operator in Java like >> , << , >>> Could anybody kindly help to explain?? And byte a =-1; a = (byte)(a>>>2); why the output become -1?? thanks.umm i did get them mixed up. preserved sign bit r

  • The Shift operator QuestionNovember 30

    Dear sir/madam I having question for the shift operator. What different between >>> and >> ? I have refer to sun tutorial, it state >>> is unsign, what does it mean? and last is if integer is 13 what anwer for 13>>8 is it mov

  • Shift Operation in Prodcution order.November 30

    How to record the shift operation in the Production order?Hi, Please try to simply your subject of posting. It is not necessary your subject and body of discussion should be same. Yes possible to remove under SQL management studio provided you have a

  • Pda reproducible bug with logical shift operation when y 3 for arraysNovember 30

    Folks, I have discovered a problem and wonder if this is a wider bug or if I am doing something wrong: Labview PDA 8.0.1  -  running on a Dell Axim: I create a 2x1000 array of U32 and feed it as x into the logical shift operator.  If  y is between -3

  • How can I do Shift operation in plsqlNovember 30

    hello, i want to know whether there is any package or operator to do the shift operation in plsql. like (myVar<<8 in c++).why don't you use myvar := mywar * power(2,8);Read other 2 answers

  • Bitwise shift operator...November 30

    I know this is very basic but I've never done this... If I have an object that is an Integer, and I want to increase it by 1, isn't this how you do it? Object ob=new Integer(10); ob=ob>>1;it tells me that >> operator cannot be applied to objec

  • X y Shift Operation not existingNovember 30

    Why is the x <<< y shift operation not existing? x >>> y is existing, so why not the opposite to it? And what is the use of these shift operations?Why can't I do this? int a = 1431655765; // 01010101010101010101010101010101 a << 1

  • Shift operationsNovember 30

    Could anyone explain me this? I am implementing some arithmetic that requires bitwise operations. What I don understand, is when I use the unsigned shift-right operation (x >>> y), the bits that are shifted in seem to be a random sequence of 0s a

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 1.126 s.