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?
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?