File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  Masking sign extention of shift opt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Masking sign extention of shift opt." Watch " Masking sign extention of shift opt." New topic
Author

Masking sign extention of shift opt.

ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
// Masking sign extention.
class HexByte
{
static public void main(String s[])
{
char hex[]=
{
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex [ (b >> 4 ) & 0x0f] + hex[b & 0x0f]);
}
}

//Here is the o/p..... output is 0xf1
I am not understading ..how the o/p is 0xf1 ...pls...explain this.BTW..wht. is masking sign extention ??
THANKS IN ADVANCE.
<marquee>RATUL </marquee>
g krishnan
Greenhorn

Joined: Mar 30, 2001
Posts: 23
hi ratul
In hex[(b>>4)&0x0f], b is changed to int before shift, which means all 24bits to the left of initial is filled with 1s, then the shift takes place, now the right extreme 4 bits will be filled with 1s, which is ANDed by 0f, which will result in f, now hex[f] is equal to f from array.
Next b is taken again it is ANDed with 0f straightaway without any shifting operation, this result only one at the right most position and all the remaining bits zero, so now what we have is hex[1], which is equal to 1 as per array.
hence the final result is f1. the prefix 0x is added by the string within quotes.
i think this will clarify
thanks
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
HI g,

THANKS g krishnan...but still now ..I am not understanding
the concept of "Masking sign extention of shift opt."

Pls..explain this.
ratul
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
Hi,
Pls..can any1 explain me wht. is:
"Masking sign extention of shift opt."
Thanks.
Pls..do ans.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
krishnan, can you tell me why the 24 bits left of the original byte are filled with 1's and not 0's? I thought they are filled with 1's only when the sign is negative. When it's positive, it's filled with 0's.
vkswamy venkatachalam
Greenhorn

Joined: Feb 18, 2001
Posts: 21

Hi,Cameron
It is representing a number in binary base.(base 2).
Integer number in java is represented by 32 bits.so when you representing a number say 2 ,it is as follows
0000 0000 0000 0000 0000 0000 0000 0010 = (0 x 1) +(1 x 2) +(0 x 4) ... and so on
when U represents a negative number takes the complement of the the (binary code )number (SAY 2) and add 1
(e.g)
complement of 2 in 32 bits =
1111 1111 1111 1111 1111 1111 1111 1101 +
0000 0000 0000 0000 0000 0000 0000 0001
====================================
1111 1111 1111 1111 1111 1111 1111 1110 = -2
=====================================
back to the Q
the MSB ie the most significant bit represents the sign of the number.
also the f = 15 in decimal base.binary reperesentation of15 =
(binary code) 1111 = (1 x 1) + (1 x 2) +(1 x 4)+(1 x 8)
when 0xf1 is converted to int (32 bits) the sign bit remains the same.
Actually it is -ve number.Ur thouught is correct.
Hope this clear Ur doubt.
Regards,
vkswami.

vkswami
Rick Zhong
Greenhorn

Joined: Apr 13, 2001
Posts: 10

hope helpful
Rick
[This message has been edited by Rick Zhong (edited April 13, 2001).]
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
THANKS ALL.
Many many thanks for ur help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Masking sign extention of shift opt.