Trying to answer to another post whose solution deals with IP addresses and netmasks, I got stuck with plain bitwise arithmetic.
Is there a standard way, in Python, to carry on bitwise AND, OR, XOR, NOT operations assuming that the inputs are "32 bit" (maybe negative) integers or longs, and that the result must be a long in the range [0, 2**32]?
In other words, I need a working Python counterpart to the C bitwise operations between unsigned longs.
EDIT: the specific issue is this:
>>> m = 0xFFFFFF00 # netmask 255.255.255.0 >>> ~m -4294967041L # wtf?! I want 255
The int data type in python simply the same as the signed integer. A signed integer is a 32-bit integer in the range of -(2^31) = -2147483648 to (2^31) – 1=2147483647 which contains positive or negative numbers. It is represented in two's complement notation.
The bitwise right shift operator in python shifts the bits of the binary representation of the input number to the right side by a specified number of places. The empty bits created by shifting the bits are filled by 0s. The syntax for the bitwise right shift is a >> n.
Python doesn't have builtin unsigned types. You can use mathematical operations to compute a new int representing the value you would get in C, but there is no "unsigned value" of a Python int. The Python int is an abstraction of an integer value, not a direct access to a fixed-byte-size integer.
You can use ctypes and its c_uint32
:
>>> import ctypes >>> m = 0xFFFFFF00 >>> ctypes.c_uint32(~m).value 255L
So what I did here was casting ~m
to a C 32-bit unsigned integer and retrieving its value back in Python format.
You can mask everything by 0xFFFFFFFF
:
>>> m = 0xFFFFFF00 >>> allf = 0xFFFFFFFF >>> ~m & allf 255L
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With