Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Right shift using array

Kranthi Kumar
Greenhorn
Posts: 11
class ByteUShift {
static public void main(String args[]) {
char hex[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
byte b=(byte) 0xf1;
byte c=(byte) (b>>4);
byte d=(byte) (b>>>4);
byte e=(byte) ((b & 0xff)>>4);
System.out.println(" b =0x" +hex[(b>>4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b>>4 =0x" +hex[(c>>4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b>>>4 =0x" +hex[(d>>4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff)>>4=0x" +hex[(e>>4) & 0x0f] + hex[e & 0x0f]);

Output is like this:
b =0xf1
b>>4 =0xff
b>>>4 =0xff
(b & 0xff)>>4 =0x0f

Can any one explain this, I got all the variables but, i don't understand the logic in the output. how(( hex[(b>>4))) works? and also for ex:first output in the program , how the first array (+hex[(b>>4) & 0x0f]) works with second array(+ hex[b & 0x0f]). wt operator is working there AND(&) or OR(|).
Any one explain it plz, thanks.

Amit Das
Ranch Hand
Posts: 206
i've remade the code..... why its giving -ive values in the o/p

the o/p is:

-15
-1
-1
15
b =0xf1
b>>4 =0xff
b>>>4 =0xff
(b & 0xff)>>4=0x0f

thanx,
amit

Simon Cockayne
Ranch Hand
Posts: 214
Hi,

I started to go through your original code:

class ByteUShift {
static public void main(String args[]) {
char hex[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
byte b=(byte) 0xf1;
byte c=(byte) (b>>4);
byte d=(byte) (b>>>4);
byte e=(byte) ((b & 0xff)>>4);
System.out.println(" b =0x" +hex[(b>>4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b>>4 =0x" +hex[(c>>4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b>>>4 =0x" +hex[(d>>4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff)>>4=0x" +hex[(e>>4) & 0x0f] + hex[e & 0x0f]);

Output is like this:
b =0xf1
b>>4 =0xff
b>>>4 =0xff
(b & 0xff)>>4 =0x0f

Can any one explain this, I got all the variables but, i don't understand the logic in the output. how(( hex[(b>>4))) works? and also for ex:first output in the program , how the first array (+hex[(b>>4) & 0x0f]) works with second array(+ hex[b & 0x0f]). wt operator is working there AND(&) or OR(|).
Any one explain it plz, thanks.

So...as I understand ***twos compliment***...it pans out like this...

****b****
byte b=(byte) 0xf1;

0xf1 = (15*16) + (1*1) = 241

241 as 32 bit int in binary is

00000000 00000000 00000000 11110001

Cast to an 8 bit byte is 11110001 = (-128 + 64 + 32 + 16 + 0 + 0 + 0 +1) = -15

B = -15 or as a 32 bit int, 11111111 11111111 11111111 11110001

****c****
byte c=(byte) (b>>4);

-15 as 32 bit int in binary is

11111111 11111111 11111111 11110001

Shifted four places to the right, keeping the sign is
11111111 11111111 11111111 11111111

= -1

hex[(b>>4) & 0x0f] + hex[b & 0x0f]);

(b>>4) & 0x0f

(b>>4) = 11111111 11111111 11111111 11111111

0x0f = 15 = 00000000 00000000 00000000 00001111

11111111 11111111 11111111 11111111 & 00000000 00000000 00000000 00001111

= 00000000 00000000 00000000 00001111

hex[15] + hex[b & 0x0f]);

b & 0x0f

b = 11111111 11111111 11111111 11110001

0x0f = 15 = 00000000 00000000 00000000 00001111

11111111 11111111 11111111 11110001 & 00000000 00000000 00000000 00001111

= 1

hex[15] + hex[1]);

hex[15] = �f�
hex[1] = �1�

It is nearly midnight here in the UK, so I trust you fill forgive the rough appearance and possible errors.

Simon

Simon Cockayne
Ranch Hand
Posts: 214
I mean "two's complement notation", sorry.

Simon.

Amit Das
Ranch Hand
Posts: 206
Hi Simon,

it's midnight in UK but still your explanation is good!!!

thanx,
amit

Kranthi Kumar
Greenhorn
Posts: 11
Thank you very much simon, I got it.

Kranthi.

Kranthi Kumar
Greenhorn
Posts: 11
Sorrry guys, still this is not clear.
class ByteUShift {
static public void main(String args[]) {
char hex[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
byte b=(byte) 0xf1;
byte c=(byte) (b>>4);
byte d=(byte) (b>>>4);
byte e=(byte) ((b & 0xff)>>4);
System.out.println(" b =0x" +hex[(b>>4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b>>4 =0x" +hex[(c>>4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b>>>4 =0x" +hex[(d>>4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff)>>4=0x" +hex[(e>>4) & 0x0f] + hex[e & 0x0f]);

Output is like this:
b =0xf1
b>>4 =0xff
b>>>4 =0xff
(b & 0xff)>>4 =0x0f

Look at the variable d.
byte d=(byte) (b>>>4);// it is unsigned right shift.

It should be positive then(( b>>>4 gives 0x0f), how come it is giving output -1?

Thanx,
kranthi.
[ March 24, 2005: Message edited by: Kranthi Kumar ]

Amit Das
Ranch Hand
Posts: 206
kranti ....i also had the same doubt yesterday but forgot to mention, thanx to you for putting it up again....

i/p's welcome ....

Animesh Shrivastava
Ranch Hand
Posts: 298

byte d=(byte) (b>>>4);

As u know the reason why u r casting the result to a byte. If u dont do it it will throw u a compilation error because [B]b>>>4[B] returns an integer value and not a byte value.
So, now going on to the question

when the statement
b>>> 4 is executed:
1) b: 1111 0001
2) But when u perform b >>> 4, it implicitly converts byte b to an int which is therefore executed as:
1111 1111 1111 1111 1111 1111 1111 0001 >>> 4 ->
->
0000 1111 1111 1111 1111 1111 1111 1111

Now cast the obtained result to an int.
U get only first eight bits which is
1111 1111--> -1 in byte

Hope now its clear now to u.
[ March 24, 2005: Message edited by: Animesh Shrivastava ]

Amit Das
Ranch Hand
Posts: 206
but animesh why are we not doing it in the same way as you've done here in this post:
http://www.coderanch.com/t/248435/java-programmer-SCJP/certification/will-sign

i mean when b is a byte type data then is it that for all shits operations we'll first have to convert it to int then perfrom the shift and then again cast to byte...

correct me if wrong...

Animesh Shrivastava
Ranch Hand
Posts: 298

i mean when b is a byte type data then is it that for all shits operations we'll first have to convert it to int then perfrom the shift and then again cast to byte...

Yes u have to first convert it to either int or long depending on both the shift value and variable.
We cannot have something like this:
<byte> >>> <integer>

Always remember that binary operation on two different type primitives cannot be performed without both of them being promoted to the broadest numeric type amongst them. I mean if one is byte and other is int we have to convert the byte variable to int and then perform the operation.

Amit Das
Ranch Hand
Posts: 206
that's fine but my first concern remains unanswered i.e. whatever has been done in that post which i've mentioned is wrong...as there no conversion to int has been done ....
just look at it

Animesh Shrivastava
Ranch Hand
Posts: 298
Where in that post u r having this kinda problem, do let me know,
i have read the post, i dont think anything is wrong there,they have just considered that they are casting to byte.
The problem in this post was regarding ">>>" operator, but if u use ">>" for the question posted here in this post u will get the same value, i mean negative byte value.
The same they are doing in the other post.

Amit Das
Ranch Hand
Posts: 206
is it that whenever we have signed right shift we wont need to convert byte to int as done there and only in case of unsigned right shift we'll do it as u've done it here......
i am confused....

Animesh Shrivastava
Ranch Hand
Posts: 298
Sorry for confusing u,
But just remember this and forget everything about shift operators:

Always remember that binary operation on two different type primitives cannot be performed without both of them being promoted to the broadest numeric type amongst them. I mean if one is byte and other is int we have to convert the byte variable to int and then perform the operation.

Now after reading this perform different oprations and check out for the output. U will certainly find no doubt

Amit Das
Ranch Hand
Posts: 206
okay and thanx for te concern
i'll do it and then get back if stuck

Kranthi Kumar
Greenhorn
Posts: 11
HI guys,
Java has a property that it will promote both short and byte ti int before it performs any operation on them.
Amit in the post u have referred, i didn't mention that but, it happens there too. there we r just right shifting the x for the remaining 3 bytes u can put 1's( int-32 bits, there i just explained with only 8 bits). So it still be promoted to int.

thanks.