unsigned right shift casts to int automatically!!!

  • Thread starter Ehsan Khoddam mohammadi
  • Start date
E

Ehsan Khoddam mohammadi

I try to unsigned right shift the byte vars,but the the U R shifting
casts the data to int and extends it to int in unsigned form. so i got
wrong answers.
consider this class and its output plz

public class shift {
static void integerShift(){
int mask=0x0080;
System.out.println(mask+" "+Integer.toBinaryString(mask));
for (int i=0;i<8;++i){
mask=(mask >>> 1);
System.out.println(mask+" "+Integer.toBinaryString(mask));
}
}
static void byteShift(){
byte mask=(byte)0x80; /* byte mask= Byte.MIN_VALUE;*/
System.out.println(mask+" "+Integer.toBinaryString(mask));
for (int i=0;i<8;++i){
mask=(byte) (mask >>> 1);
System.out.println(mask+" "+Integer.toBinaryString(mask));
}
}

public static void main(String[] args) {
System.out.println("Integer Shifting");
integerShift();

System.out.println("Byte Shifting");
byteShift();

}
}

/*********** output ************/

/*Integer shifting*/

128 10000000
64 1000000
32 100000
16 10000
8 1000
4 100
2 10
1 1
0 0

/*Byte shifting */

-128 11111111111111111111111110000000
-64 11111111111111111111111111000000
-32 11111111111111111111111111100000
-16 11111111111111111111111111110000
-8 11111111111111111111111111111000
-4 11111111111111111111111111111100
-2 11111111111111111111111111111110
-1 11111111111111111111111111111111
-1 11111111111111111111111111111111

the desired output is first one ("Integer shifting") but it uses int.
you see, in second method ("byteShift()"), the result of shifting casts
to int automatically so it's not what i desire. is it any direct way to
unsigned right shift the bytes?
 
P

Patricia Shanahan

Ehsan said:
I try to unsigned right shift the byte vars,but the the U R shifting
casts the data to int and extends it to int in unsigned form. so i got
wrong answers.

Arithmetic operations with byte operands are required by the JLS to do
"numeric promotion". Specifically, in the case of the shift operators:

"Binary numeric promotion (§5.6.2) is not performed on the operands;
rather, unary numeric promotion (§5.6.1) is performed on each operand
separately. The type of the shift expression is the promoted type of the
left-hand operand."

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121

The unary numeric promotion for byte is a cast to int, which does sign
extension.

You can do an explicit cast followed by a mask to kill the sign extension:

static void byteShift(){
byte mask=(byte)0x80; /* byte mask= Byte.MIN_VALUE;*/
System.out.println(mask+" "+Integer.toBinaryString(mask));
for (int i=0;i<8;++i){
mask=(byte) (((int)mask & 0xff) >>> 1);
System.out.println(mask+" "+Integer.toBinaryString(mask));
}
}

Alternatively, do most of your work in int, and only switch to byte when
you really need to.

Patricia
 
E

Ehsan Khoddam mohammadi

thanx, nice hints
Patricia said:
Arithmetic operations with byte operands are required by the JLS to do
"numeric promotion". Specifically, in the case of the shift operators:

"Binary numeric promotion (§5.6.2) is not performed on the operands;
rather, unary numeric promotion (§5.6.1) is performed on each operand
separately. The type of the shift expression is the promoted type of the
left-hand operand."

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121

The unary numeric promotion for byte is a cast to int, which does sign
extension.

You can do an explicit cast followed by a mask to kill the sign extension:

static void byteShift(){
byte mask=(byte)0x80; /* byte mask= Byte.MIN_VALUE;*/
System.out.println(mask+" "+Integer.toBinaryString(mask));
for (int i=0;i<8;++i){
mask=(byte) (((int)mask & 0xff) >>> 1);
System.out.println(mask+" "+Integer.toBinaryString(mask));
}
}

Alternatively, do most of your work in int, and only switch to byte when
you really need to.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top