delorie.com/archives/browse.cgi | search |
On Fri, Jan 31, 2014 at 08:07:09PM +0000, Rupert Swarbrick wrote: > Алексей Харьковский <svetonomer-Re5JQEeQqe8AvxtiuMwx3w AT public DOT gmane DOT org> > writes: > > This is simple: > > In [20]: ~0 > > Out[20]: -1 > > For other readers who (like me) are being slow today, the ~ operator > performs unary bitwise NOT. Apparently, integers in Python are > explicitly two's complement. I don't even know if there are still non two's complement machines being manufactured. And if these exotic beasts do exist, Python has probably not been ported to them. Basically today, all machines you can afford to buy use 2's complement integer, have word sizes which are a power of 2, and use IEEE-754 floating point. Well, IBM mainframes (not that an individual can afford one) still allow to use the original S/360 hexadecimal FP format, but they also support IEEE FP (plus decimal floating point for comercial workloads). This said, there is another important point: for an array of n elements, valid indices in Python are in the range from -n to n-1. The range 0 to n-1 is the familiar zero-based indexing used in C and other languages, negative indices run backwards from the end of the array so a[-n] is the first element and a[-1] the last. [Note that I don't think that this definition is a good idea: it may hide bugs when you have a complex indexing expression which is off by one and the Python interpreter will happily access the last element of an array instead of throwing an exception for out of bounds access. There are cases where it is handy but I am of the strong opinion that it is more dangerous than useful.] Gabriel
webmaster | delorie software privacy |
Copyright 2019 by DJ Delorie | Updated Jul 2019 |