• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

what will be the sign!!

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
Assume that bit pattern of byte x is 10110011, what will the sign x be after x>>2.
thanks,
sri.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wont this give a compile time exception.
Possible loss of precision
byte x = 179;//10110011; ??
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No srini,
it wont give any compilation error.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maximum positive value a bye can hold is 127 then how come 179 ?

10110011 --> 179.
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it wont shift 01001101

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



S.D.Balasubramani,
SCJP 1.4
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kranthi , Good i got it.
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic