# Masking sign extention of shift opt.

ratul banji

Ranch Hand

Posts: 108

posted 14 years ago

- 0

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

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

posted 14 years ago

- 0

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

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

ratul banji

Ranch Hand

Posts: 108

Cameron Park

Ranch Hand

Posts: 371

vkswamy venkatachalam

Greenhorn

Posts: 21

posted 14 years ago

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.

- 0

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

Posts: 10