Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Masking sign extention of shift opt.

 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Pls..can any1 explain me wht. is:
"Masking sign extention of shift opt."
Thanks.
Pls..do ans.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rick Zhong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hope helpful
Rick
[This message has been edited by Rick Zhong (edited April 13, 2001).]
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS ALL.
Many many thanks for ur help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic