aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what will be the sign!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what will be the sign!!" Watch "what will be the sign!!" New topic
Author

what will be the sign!!

sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hello All,
Assume that bit pattern of byte x is 10110011, what will the sign x be after x>>2.
thanks,
sri.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Wont this give a compile time exception.
Possible loss of precision
byte x = 179;//10110011; ??


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
No srini,
it wont give any compilation error.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Maximum positive value a bye can hold is 127 then how come 179 ?

10110011 --> 179.
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi srini,
this question has been given in the java rule round up game.
and the ans they have given is negative.
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi srini,
this question has been given in the java rule round up game.
and the ans they have given is negative. I dont know, how this becomes negative.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
This code wont compile.


Can you just paste the code you saw in round up game.

The concept goes this way :
Then when you are using the signed right shift operator ( >> ) the value of sign bit is maintained . The new bit added after the shift in the left side will be the same as before ( to be more clear the value of sign bit is maintained as such.)

PS : you can use a Pencil like icon for editing the previous post instead creating a new .
[ March 24, 2005: Message edited by: Srinivasa Raghavan ]
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
hi friend,
if byte variable contains the bits 10110011 means it is not 179.

if the sign bit (leftmost bit) is one means the value stored inside that variable is negative . in this case the value inside the variable is found as follows ,

10110011
(-) 1
-------------
10110010
after that get 1's complement for the result

i.e 01001101 = 77

actually the value stored in x is -77


after x>>2

11011001 (1st shift)
11101100 (2nd shift)

(>> operator wont shift sign bit)

so, the value of x>>2(11101100) is negative (becoz here left most bit is 1)


S.D.Balasubramani
SCJP 1.4


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
But after getting 77 ( after 2's compliment ) why are you again finding the 1's compliment & shifting twice. Can you plkease explain
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
it wont shift 01001101

actually byte inside the x is 10110011.
this bits only will be shifted



S.D.Balasubramani,
SCJP 1.4
Kranthi Kumar
Greenhorn

Joined: Jan 14, 2005
Posts: 11
HI Sri,

Notice that the byte is starting with 1 (consider it as signed byte).
x is 10110011( it is -77)
After x>>2, move from the left to 2 bits forward, then the two 1's in the rightmost side will be lost.
It is like this

--101100, the two emply spaces in the left will be filled with 1. So the result of x after x>>2 is
11101100.
You would be confused why the high order bits(left most bit in a byte is called high order bit) r filled with 1?
When you are shifting right, the leftmost bits exposes by the right shift are filled in with the previous contents of the top bit, and the as many number of bits in the right will be discarded.This is called sign extension and serves to preserve the sign of negative numbers when u shift them right. This happens only in right shift but not in left shift.

Finally coming to ur example
10110011 is -77
>>2
11101100 is -20
after x>>2 -77 has become -20.

cheers,
Kranthi.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
So the following steps are followed on shifting a negative number
1. Find 2's compliment,
2. Find 1's compliemt for the result
3. Then Shift depending upon the shift operator .

Is that right ?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Kranthi , Good i got it.
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi Kranthi,
is this is the way for all the negative numbers?
like whether it is an int or short or long.

when we right shift a negative number, what ever the last bit, whether it is 0 or 1, the same will be replaced for the left most bits right.

am i correct kranthi?
Kranthi Kumar
Greenhorn

Joined: Jan 14, 2005
Posts: 11
Hi Sri,

Yes, what ever the int type it is, the high order bit is replaced by the previous top bit bit. either it is 0 or 1.This is to preserve the sign of the number.

regards,
kranthi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what will be the sign!!