programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
• Paul Clapham
Sheriffs:
• paul wheaton
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Piet Souris
Bartenders:
• Mike London

# Masking sign extention of shift opt.

Ranch Hand
Posts: 108
• Number of slices to send:
Optional 'thank-you' note:
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 ??
<marquee>RATUL </marquee>

Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Pls..can any1 explain me wht. is:
"Masking sign extention of shift opt."
Thanks.
Pls..do ans.

Ranch Hand
Posts: 371
• Number of slices to send:
Optional 'thank-you' note:
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.

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:

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.

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:

Rick
[This message has been edited by Rick Zhong (edited April 13, 2001).]

ratul banji
Ranch Hand
Posts: 108
• Number of slices to send:
Optional 'thank-you' note:
THANKS ALL.
Many many thanks for ur help.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?