Blowfish in Python?

Michael Zawrotny zawrotny at gecko.sb.fsu.edu
Wed Mar 1 15:18:36 CET 2000


On Wed, 1 Mar 2000, Richard Brodie <R.Brodie at rl.ac.uk> wrote:
> 
> "Michael Zawrotny" <zawrotny at gecko.sb.fsu.edu> wrote in message
> news:slrn8bnveq.1jn.zawrotny at gecko.sb.fsu.edu...
> > In an ideal world, perhaps not.  It does seem to be one of those places
> > that the standard allows implementors some leeway in behavior.  K&R 2nd
> > Ed. p. 49 says:
> 
> Yes but I hope we're not into saying - if 'C' does it it must be right ;)
> Java, for example, has two right shift operators, so the behaviour is
> well defined. The Python documentation is fairly clear that sign
> extension is required; so the C implementation, at least, looks broken
> in that it relies on undefined behaviour.

Others had pointed out that for ints, the python operators were simply
calling the C versions.  My point was that for right shift, a particular
implementation was allowed to do either arithmetic or logical shif.  Note
that this is implementation defined behavior (with only two choices), not 
undefined behavior.  

It may seem like nitpicking, but it is an important distinction.  As
specified, an implementation can pick to fill either with zero bits or
with sign bits.  If the behavior were undefined, the implemenation would
be free to ignore the statement, dump core, wipe your filesystem or anything
else it felt like doing.

I don't think that I would call the C behavior "broken" although I would
agree that it is a pain for portability and a definite "gotcha" to be aware
of when writing software.

> When I try with ints and long ints, I get:
> 
> 0xffffffff >> 30 = -1
> 0xffffffffL >> 30 = 3L
> 
> Which is perhaps unexpected but having -1 >> 30 different from
> -1L >> 30 is worse, IMHO. I think the heart of the problem is that
> 0xffffffff != 0xffffffffL. Presumably the literal 0xffffffff isn't -1 on a
> 64bit Python either.

I'm not qualified to comment on the details of the python implementation or
documentation.  However, I would like to note that in my experience, when
someone specifies 0xffffffff for an int the intent is usually "give me an
int sized chunk of memory filled with 1 bits".  If the intent were to have
the value -1, 0xffffffff would be the worst way to specify it since that
wouldn't necessarily hold true on anything other than a 32-bit two's 
complement machine.


Mike

-- 
Michael Zawrotny
411 Molecular Biophysics Building
Florida State University		| email:  zawrotny at sb.fsu.edu
Tallahassee, FL 32306-4380		| phone:  (850) 644-0069



More information about the Python-list mailing list