Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

left shift

Thread view: 
devnew@gmail.com - 22 Oct 2007 10:37 GMT
hi
i am relatively new to python..was checking out some bit shifting..
when i do
255<< 24  i get  4278190080

whereas in java or c it gives  -16777216

can someone tell me why this is ?,what should i do if i want the
output as -16777216 ?

dn

p:s
I wrote a fn to show the binary digits in the number and it gives same
output for both numbers(4278190080 and -16777216)
def pBinInt(str,i):
   print str,",int",i,", binary:"
   print "  ",
   for j in range(31,-1,-1):
       if(((1 << j) &  i) != 0):
           print 1,
       else:
           print 0,
   print ""

#example usage pBinInt("255",255)
Daniel Dyer - 22 Oct 2007 10:51 GMT
> hi
> i am relatively new to python..was checking out some bit shifting..
[quoted text clipped - 5 lines]
> can someone tell me why this is ?,what should i do if i want the
> output as -16777216 ?

Because you are using a 32-bit int and in Java all ints are signed (the  
most significant bit is negative).  Presumably the Python value is an  
unsigned 32-bit type?

Use a long to avoid this (long is signed too but if you are not using the  
full range of values it won't cause you a problem).

> p:s
> I wrote a fn to show the binary digits in the number and it gives same
> output for both numbers(4278190080 and -16777216)

That's because they are the same in binary, it's just how the most  
significant bit is interpretted that is different.

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

Daniel Pitts - 22 Oct 2007 15:53 GMT
>> hi
>> i am relatively new to python..was checking out some bit shifting..
[quoted text clipped - 9 lines]
> most significant bit is negative).  Presumably the Python value is an
> unsigned 32-bit type?
Actually, Python values are more closely related to BigIntegers. They
grow to avoid overflow.

> Use a long to avoid this (long is signed too but if you are not using
> the full range of values it won't cause you a problem).
[quoted text clipped - 5 lines]
> That's because they are the same in binary, it's just how the most
> significant bit is interpretted that is different.
Specifically, Java interprets signed numbers as twos complement.
<http://en.wikipedia.org/wiki/Two's_complement>

Good luck,
Daniel.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Ulrich Eckhardt - 22 Oct 2007 11:20 GMT
> i am relatively new to python..was checking out some bit shifting..
> when i do
> 255<< 24  i get  4278190080

..which is correct, assuming a left-shift means that the value is multiplied
by 2 raised to the power of 24, although it is not common because usually
the behaviour is inherited from the CPU which uses a limited amount of
digits.

> whereas in java or c it gives  -16777216

..which may or may not be correct, depending on the definition. At least for
C, I believe that this causes "undefined behaviour", which is why you
should avoid using shift operations on signed types there, rather use
unsigned types. I guess that Java's VM is defined so that its shift
operations assume twos complement representations of signed integers. Can
anyone clarify that?

Further, Python as opposed to C or Java is dynamically typed. That means
that the results of

 255 << 20
 255 << 30

not only yield different values but also different types! This gives Python
the ability to switch to a bigger representation type where it would
overflow in C or Java. See the output of

 type(255 << 20)
 type(255 << 30)

for example.

> I wrote a fn to show the binary digits in the number and it gives same
> output for both numbers(4278190080 and -16777216)
[quoted text clipped - 9 lines]
>
> #example usage pBinInt("255",255)

This is due to the fact that both are represented by the same sequence of
bits, which are only interpreted differently. Other interpretations would
be as a floating point number or a sequence of characters.

Uli

Signature

Sator Laser GmbH
Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932

devnew@gmail.com - 22 Oct 2007 14:41 GMT
thanx for the help.. guys..
dn
Patricia Shanahan - 22 Oct 2007 14:43 GMT
>> i am relatively new to python..was checking out some bit shifting..
>> when i do
[quoted text clipped - 13 lines]
> operations assume twos complement representations of signed integers. Can
> anyone clarify that?
...

Java operator behavior is specified in the Java Language Specification,
so the same rules apply regardless of whether Java is implemented using
the JVM or not.

">>" does two's complement sign extension. See the JLS, 15.19 Shift
Operators, at
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.19

Java does have another right shift, ">>>", that does zero fill and has
the effect of an unsigned right shift.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.