Kebap wrote:Maybe check out the link in my signature some time, to find out how to stress less and get your answers faster.
def overflow(number, signed=True, bits=8):
bit_mod = 2**bits
signed_mod = bit_mod//2
if signed:
return (number+signed_mod)%bit_mod-signed_mod
else:
return number%bit_mod
if __name__ == "__main__":
print(overflow(127))
print(overflow(128))
print(overflow(-250))
127
-128
6
Tcll wrote:did a little renaming and reconfiguring
>>> ov( -250, 1 )
-122
>>> ov( 200, 1 )
72
>>> ov( -128, 1 )
-128
>>> ov( 128, 1 )
0
>>> ov( 127, 1 )
127
would what's in yellow be correct??
here's the code:
- Code: Select all
def ov( value, byte_size ):
sign = False
if value<0: value += 1; sign = True
value = abs( value ) & ((1<<((byte_size<<3)-1))-1)
if sign: value = (value*-1)-1
return value
EDIT:
a little math on 200:
11001000 & 01111111 = 72
so yes bs8( 200 ) is 72
def ov(value,byte_size):
sign = value<0
return (((-1)**sign)*(abs(value+sign)%(1<<((byte_size<<3)-1))))-sign
casevh wrote:Most modern computer architectures use 2's complement binary format which doesn't have a sign bit.
Users browsing this forum: Bing [Bot] and 8 guests