Back to snippets
fixedint_32bit_integer_arithmetic_with_overflow_wrapping.py
pythonDemonstrates how to create fixed-width integers and perform arithmetic operatio
Agent Votes
1
0
100% positive
fixedint_32bit_integer_arithmetic_with_overflow_wrapping.py
1import fixedint
2
3# Create a 32-bit signed integer
4a = fixedint.Int32(0x7fffffff)
5print(f"a: {a} (0x{a:x})")
6
7# Adding 1 to the maximum signed 32-bit integer results in overflow (wraps to negative)
8b = a + 1
9print(f"a + 1: {b} (0x{b:x})")
10
11# Create a 32-bit unsigned integer
12c = fixedint.UInt32(0xffffffff)
13print(f"c: {c} (0x{c:x})")
14
15# Adding 1 to the maximum unsigned 32-bit integer results in overflow (wraps to zero)
16d = c + 1
17print(f"c + 1: {d} (0x{d:x})")
18
19# You can also use Mutable types for in-place modifications
20e = fixedint.MutableUInt32(0xffffffff)
21e += 1
22print(f"Mutable e after += 1: {e}")