Nick Coghlan wrote:
When not working at the hardware level, just go with the definition:

Py> def complement(val, limit=256):
...   if val >= limit or val < 0:
...     raise ValueError("Value out of range for complemented format")
...   if val == 0:
...     return 0
...   return limit - val
...
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(0x55, 256*256))
'0xffab'

Or there's the simplest definition:

Py> def complement(val, limit=256):
...   return (limit - val) % limit
...
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(-0x55))
'0x55'
Py> hex(complement(0xab))
'0x55'
Py> hex(complement(-0xab))
'0xab'

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
            http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to