Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about shift operators

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

i want to know how we are calculating 127>>-1 .i know if it is 1 on the right hand side we can do it by dividing first by second .but what is happening here if it a negative value .please help

one more question how can i do 127>>>4 and -128>>>3 also with some negative values

thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been asking many questions here -- and this is, at least, the third one that needs an understanding of Twos Complement.

So do you understand Twos Complement yet? You never responded to your previous questions, so we don't know if you actually understood the answers.

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry dear actually what happened is that i am not able to know how to ask another question in the same topic that is why i understood whatever you told i know two's complement .but when negative number came no idea what to do
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
about the first question

how can i shift it using -1 ?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
about the first question

how can i shift it using -1 ?


When you shift an int by an amount, only the 5 lower bits (of the amount) are used. (for long, it is the 6 lower bits)

And what is the 5 lower bits for -1? Well, since you now understand Twos complement, can you tell me what the 5 lower bits for -1 is?

Henry
[ September 09, 2008: Message edited by: Henry Wong ]
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it is 11111 am i right

if i am wrong truly i say sorry ?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
i think it is 11111 am i right

if i am wrong truly i say sorry ?


Yup, so shifting an int by -1, is the same as shifting the int by 31 !!

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok buddy
if the number is other than -1 like 134>>-2,-3 and so on then what to do.
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
ok buddy
if the number is other than -1 like 134>>-2,-3 and so on then what to do.


Same thing. What is the bit pattern (using Twos Complement to figure out) of -2 or -3? What is the 5 lower bits of that value / pattern?

Henry
[ September 09, 2008: Message edited by: Henry Wong ]
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup i got it what is happening if i use >>> ?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
yup i got it what is happening if i use >>> ?


In a ">>" right shift, the high order bit remains unchanged during the shift. In a ">>>" right shift, the high order bit is cleared -- mean a zero is shifted into it.

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks dear
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
while doing this i have some problem ? 340<<-6
after two's compliment i got like this 1111 1010 and i took the first 5 lower bits and in decimal it is 26 .
when i multiply 340*2 raised to 26 manually i am getting value 22817013760 .but the output compiler is showing to me is 1342177280

i want to know why ?if am wrong in doing please help me out

thanks
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if am using 340<<-1 i am getting 0 as output. but it is multiplication right 340*2 raised to 31 how ?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i multiply 340*2 raised to 26 manually i am getting value 22817013760 .but the output compiler is showing to me is 1342177280


Keep in mind, that shifting left is *not* the same as multiplying by two. Multiplying by two is just a technique that some teachers use to get to the "answer" -- which in my opinion... well, I'll get to that next.

Shifting left is... well... shifting left. To envision it, you need to see it in binary. So, lets start with 340:

340 = 0000 0000 0000 0000 0000 0001 0101 0100

If you shift this left by 26, you will need to actually shift it left by 26. Also note that an int can only hold 32 bits, so when you shift left, stuff "falls off" on the left side.

So... Shifting left by 26 bits, will add 26 zeros on the right, move the lower order bits up, and 26 of the higher order bits will "fall off"....

304 << 26 = 0101 0000 0000 0000 0000 0000 0000 0000

Notice that some of the bits have fallen off. Anyway, I leave it up to you to convert this result back to decimal.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
and if am using 340<<-1 i am getting 0 as output. but it is multiplication right 340*2 raised to 31 how ?


Same as the last one. Start with 340 in binary...

340 = 0000 0000 0000 0000 0000 0001 0101 0100

Shift it left by 31

340 << 31 = 0000 0000 0000 0000 0000 0000 0000 0000

This will mean that all but the LSB has "fallen off", with the former LSB now being the MSB. Anyway, this result is much easier to convert back to decimal...

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind, that shifting left is *not* the same as multiplying by two. Multiplying by two is just a technique that some teachers use to get to the "answer" -- which in my opinion... well, I'll get to that next.


Oops, almost forgot about my first point. If your school offers a course in assembly language, I highly recommend that you take it. And when you do, take it seriously, even though it will be unlikely you will be coding in it professionally.

Assembly language is the best way to understand the architecture of the computer, particularly shifting techniques -- and you won't need these silly analogies like "shifting left is like multiplying by two" anymore.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
thanks dear


Oh.... And please *don't* call me "dear". Only my wife does that -- and I don't even like it when she does it.

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup i am getting the compiler answer as output

thanks
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i using 340<<-6 i am getting 1342177280
ok and if i use -340<<-6 can i write directly the answer by putting a negative sign without checking anything. is it true
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
if i using 340<<-6 i am getting 1342177280
ok and if i use -340<<-6 can i write directly the answer by putting a negative sign without checking anything. is it true


Well, what happens when you try it? You have the techniques to try it out yourself.

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope i have one doubt if we are left shifting that -340 by 26 that 26 bit are filled by 0 or 1 ?
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more problem 670<<-4
ie 670<<28
670 is 0000 0000 0000 0000 0000 0010 1001 1110

if left shifting it will get 1110 0000 0000 0000 0000 0000 0000 0000

1*2^31 + 1*2^30 +1*2^29 right
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
nope i have one doubt if we are left shifting that -340 by 26 that 26 bit are filled by 0 or 1 ?


Well, what happens when you try it? Again, you have the techniques to try it out yourself.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if left shifting it will get 1110 0000 0000 0000 0000 0000 0000 0000

1*2^31 + 1*2^30 +1*2^29 right


Nope. Sign bit is on. Remember to apply Twos Complement?

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
304 << 26 = 0101 0000 0000 0000 0000 0000 0000 0000
304<<-6
then why here we didn't take the two's compliment
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then why here we didn't take the two's compliment


You did (there was just nothing extra that needs to be done). Twos complement for positive numbers behaves the way that you already know. It's the negative numbers that needs the special handling.

Henry
[ September 10, 2008: Message edited by: Henry Wong ]
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am confused let me try it with examples .if i am not getting it i will come back again ok
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the negative numbers that needs the special handling.


BTW, this "special handling" is only for the developer. It is needed to convert negative numbers from binary to decimal, and vice-versa. A computer operates completely in binary, and doesn't need to deal with decimals during calculations.

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am working out examples and i halt here
i am doing this one 440 <<12 i got it correctly

but when i am doing -440<<12 i got wrong

i done like this

1. 440 = 0000 0000 0000 0000 0000 0001 1011 1000
and for
-440
i invert it
1111 1111 1111 1111 1111 1110 0100 0111
then i add one
1111 1111 1111 1111 1111 1110 0100 1000
now this is -440 right

so i shifted 12 bits
and got 1111 1111 1110 0100 1000 ok remaining bits are 0 or 1 after this what to do ?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok remaining bits are 0 or 1


Again, what happened when you tried it? Try it with simplier numbers.

BTW, I don't know why you are stuck at this. All the shifting examples shown so far, showed you whether it was a zero or one.

Henry
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i got it clearly
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i posted this question already.but not getting a good answer .so i am again posting it here .i want to know how i can convert a floating point value to IEEE floating point standard
for eg: 2.25

they represent it like this
s e m
0 1000 0000 001 0000 0000 0000 0000 0000
but i don't know how to do that please help

thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anto telvin Mathew:
hi
i posted this question already.but not getting a good answer .so i am again posting it here


Don't do this. A conversation has started there so continue it in there, otherwise you may end up wasting peoples time giving answers that have already been given.
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the mistake
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic