decode_long(): Simplified the "is it negative?" test.

This commit is contained in:
Tim Peters 2003-01-27 23:51:11 +00:00
parent f29d3d6011
commit 217e571a19
1 changed files with 5 additions and 2 deletions

View File

@ -590,14 +590,17 @@ def decode_long(data):
-256L -256L
>>> decode_long("\x00\x80") >>> decode_long("\x00\x80")
-32768L -32768L
>>> >>> decode_long("\x80")
-128L
>>> decode_long("\x7f")
127L
""" """
x = 0L x = 0L
i = 0L i = 0L
for c in data: for c in data:
x |= long(ord(c)) << i x |= long(ord(c)) << i
i += 8L i += 8L
if i and (x & (1L << (i-1L))): if data and ord(c) >= 0x80:
x -= 1L << i x -= 1L << i
return x return x