• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding shift operators

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,



When i executed this code, I got the answer as -1 .

Why is this so?? I thought the answer would be 0.
but when x=1
x>>1
the answer was 0.

Why do we get this difference?
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-1 => 1111 1111 1111 1111 1111 1111 1111 1111

1111 1111 1111 1111 1111 1111 1111 1111 >> 1

1111 1111 1111 1111 1111 1111 1111 1111

This is -1 .

1 => 0000 0000 0000 0000 0000 0000 0000 0001

0000 0000 0000 0000 0000 0000 0000 0001 >> 1

0000 0000 0000 0000 0000 0000 0000 0000

This is 0 .
[ March 22, 2005: Message edited by: rathi ji ]
 
Ajay Bhargov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Tnx for the reply....

I hve one more question...

BitwiseLeft shift: While doing bitwise left shift I hve read that the MSB will be preserved. I too checked it out and it worked fine





The answer tht i got when i executed was 2.From this example i came to a conclusion that the MSB bit will be preserved.

But the real problem is that when i executed this particular code, i was surprised



When i ran this particular code snippet i was astonished to see tht it gave me a negative value......

Why is this so . Why is the MSB not preserved here......

Do plz comment on this.....
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,


BitwiseLeft shift: While doing bitwise left shift I hve read that the MSB will be preserved. I too checked it out and it worked fine


I guess it should be that when u do a right shift then ur MSB is preserved, thats how it should happen.

If ur statement is true then doing a left shift on
1000 0000 0000 0000 0000 0000 0000 0000 << 1 would have given u the same value. right? but thats not the case, the bit falls of the end and the result comes to 0.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



x=1; x<<1; System.out.println(x);


The answer tht i got when i executed was 2.From this example i came to a conclusion that the MSB bit will be preserved.



how you came to know to this conclusion ???
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajay,

code 1:
--------------------------------------------------------------------------------

x=1; x<<1; System.out.println(x);

--------------------------------------------------------------------------------

code 2:
--------------------------------------------------------------------------------

x=1; x<<31; System.out.println(x);

--------------------------------------------------------------------------------

Code 1 returns 2 as answer and code 2 returns a negative value because left shift just performs the shift but dosent preserve the MSB. Its the right shift operator ">>" that preservs the MSB and if you want simple right shift operator that dosent preserve the MSB, you can make use of operator ">>>" which is called "unsigned right shift " operator. Hope this helps.

Pooja
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have some doubt on this . for example how do i shift a negative number ??

(Example)
int x = -567;
x >>=3;
System.out.prinln(x) ---> output is -71

can some one explain how the output is arrived . i am bit confused with positive & negative number shift.
 
Ajay Bhargov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Subramanian,

-567 ==> 1111 1111 1111 1111 1111 1101 1100 1001

-567>>3 ==> 1111 1111 1111 1111 1111 1111 1011 1001 ==>-71

-71 ==> 1111 1111 1111 1111 1111 1111 1011 1001

The most important point that you hve to note down is that the negative value is found out by taking the ones compliment and adding 1 to it

Consider this example

I hope you will be knowing the way to represent 567 in bits

567 ==> 0000 0000 0000 0000 0000 0010 0011 0111

The MSB will be 0 for positive numbers and will be 1 for negative numbers.

So the value of -567 is found out by taking the ones compliment of 567 and adding 1 to it

So the value of -567 will be

-567 ==> 1111 1111 1111 1111 1111 1101 1100 1001

Hope u hve got it.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
can i know how to calculate the decimal of this

1111 1111 1111 1111 1111 1111 1011 1001

MSb denotes it is negative number.
but how to cal the dec as 71

any one???
 
Ajay Bhargov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Ur question is given the binary equivalent of a negative number,how to get the decimal value

say u hve the binary equivalent of a negative number

1111 1111 1111 1111 1111 1111 1011 1001

to get the decimal equivalent of this number
Step 1:-u hve to first see the value of the MSB.By lookking at the above statement u can come to a conclusion tht the given binary value is indeed a negative number.

Step 2:- get the ones compliment of the number
So the 1's compliment of the above number will be
0000 0000 0000 0000 0000 0000 0100 0110

Step3 :- then add 1 to the LSB
so finally the value will be
0000 0000 0000 0000 0000 0000 0100 0111
from this i thnk u can find the decimal equivalent of the number
the decimal equivalent of the above number is 71

Step4:- Finally prefix "-" sign to the above number
therefore the final answer would be -71.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this link
HTH
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please bear with my ignorance on this subject but i have to learn sometime. so i guess the time has come.

Can you please answer these questions which might be flimsy/funny to most of you.

1. in some posts

is represented in binary as 0001 but in some they are represented as 0000 0000 0000 0000 0000 0000 0000 0001. what is the difference? when do you represent one or the other
2. is it really worth investing time in understanding how bit shift operators work and where do you use them in real time. I develop webappplications in the healthcare vertical using struts(jsp/servlets)/spring/hibernate/(and/or)ejb but i've never come across having to use bit shift operators and it gives me goose bumps as for me i feel it doesnt add value since i wouldnt have to ever use while developing applications.
3. but since i decided to take a java certification (starting with 1.4) i probably need to know how these operators work.. can somebody tell me how many questions one might have to solve on bit shift operators in the real exam?
thanks for reading thru my questions. i truly appreciate your patience.
reply
    Bookmark Topic Watch Topic
  • New Topic