# Python: unsigned 32 bit bitwise arithmetic

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
``````

Asked by: Melissa910 | Posted: 28-01-2022

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
``````

Answered by: Elise428 | Posted: 01-03-2022

``````from numpy import uint32
``````

Answered by: Kelvin605 | Posted: 01-03-2022

You could also xor with 0xFFFFFFFF, which is equivalent to the "unsigned complement".

``````>>> 0xFFFFFF00 ^ 0xFFFFFFFF
255
``````

Answered by: Elian672 | Posted: 01-03-2022

This is a module that I created a long time ago, and it might be of help to you:

IPv4Utils

It provides at least a `CIDR` class with subnet arithmetic. Check the test cases at the end of the module for examples.

Answered by: Patrick933 | Posted: 01-03-2022

# Similar questions

doing unsigned char arithmetic in python

Goodday, I need to port the following C code in Python, but I can't find a good way to do the unsigned char arithmetic that ignores the overflow bits in python, ie. 255+1=0; 255+2=1 etc. The code below is used in a checksum calculation for a protocol that is implemented in C on the Arduino that I need to interface with. unsigned char b[length]; unsigned char c1=0,c2=0,c3=0,c4=0; for (i=0; i&lt;length;i+...

math - How to do arithmetic right shift in python for signed and unsigned values

Some languages like Java, Verilog have both bitwise logical (&lt;&lt;, &gt;&gt;) and arithmetic shift (&lt;&lt;&lt;, &gt;&gt;&gt;) operators. For unsigned values, logical and arithmetic shifts have identical operation. Say if 8'b11000101 is binary representation of 8-bit unsigned number 197, then 8'b11000101 &gt;&gt; 2 =&gt; 8'b00110001 8'b11000101 &gt;&gt;&gt; 2 =&gt; 8'b00110001 8'...

floating point - Doing arithmetic with up to two decimal places in Python?

I have two floats in Python that I'd like to subtract, i.e. v1 = float(value1) v2 = float(value2) diff = v1 - v2 I want "diff" to be computed up to two decimal places, that is compute it using %.2f of v1 and %.2f of v2. How can I do this? I know how to print v1 and v2 up to two decimals, but not how to do arithmetic like that. The particular issue I am t...

math - Basic python arithmetic - division

I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter what I try - I'm getting 0 or 0.0 : &gt;&gt;&gt; count = friends.count() &gt;&gt;&gt; print count 1 &gt;&gt;&gt; per_page = 2 &gt;&gt;&gt; print per_page 2 &gt;&gt;&gt; pages = math.ceil(count/per_pages) &gt;&gt;&gt; print pages 0.0 &gt...

python - Numpy Modular arithmetic

How can I define in numpy a matrix that uses operations modulo 2? For example: 0 0 1 0 1 0 1 1 + 0 1 = 1 0 Thanks!

Python - question about decimal arithmetic

I have 3 questions pertaining to decimal arithmetic in Python, all 3 of which are best asked inline: 1) &gt;&gt;&gt; from decimal import getcontext, Decimal &gt;&gt;&gt; getcontext().prec = 6 &gt;&gt;&gt; Decimal('50.567898491579878') * 1 Decimal('50.5679') &gt;&gt;&gt; # How is this a precision of 6? If the decimal counts whole numbers as &gt;&gt;&gt; # part of the precision, is that actually sti...

Function arithmetic library for python

I'm searching for a library that will let me manipulate functions with the standard operators (*, -, *, /, etc.). Lets suppose you have a function f(x) = x ** 2 and g(x) = x + 2. I'd like to be able to write f * g and get a new functor that is essentialy x ** 2 * (x + 2) or f(g) and get (x + 2) ** 2. I know this is not too hard to...

C Pointer arithmetic in Python

I'm trying to convert a simple C program into Python but as I don't know anything about C and a little about Python its just difficult for me.. I'm stuck at C pointers. There is a function that takes an unsigned long int pointer and adds its values to some variables within a while-loop: uint32_t somename(const uint32_t *z) { while(....) { a += z b += z c += ...

python - Applying arithmetic functions to elements of sublists where the first element of each sublist is the same

Here is my problem. I have a list of lists, as follows: linesort=[ ['Me', 1, 596], ['Mine', 1, 551], ['Myself', 1, 533], ['Myself', 1, 624], ['Myself', 1, 656], ['Myself', 1, 928], ['Theirs', 1, 720], ['Theirs', 1, 1921], ['Them', 1, 716], ['Themselves', 1, 527] ] Each of the sublists represents the time taken by participant to classify the word, eithe...

math - How do I use accurate float arithmetic in Python?

python - Django Template Arithmetic

In my template, I am looping through a list, trying to make a two-column layout. Because of the desired two-column layout, the markup I need to write in the for loop is dependent on whether forloop.counter0 is even or odd. If I had the full power of Python in the template language, determining the parity of forloop.counter0 would be trivial, but unfortunately that is not the case. How can I te...

python - Comparing None with built-in types using arithmetic operators?

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; None &gt; 0 False &gt;&gt;&gt; None == 0 False &gt;&gt;&gt; None &lt; 0 True Is comparing None using arithmetic operators well defined for built-in types (integers in this case)? Is the difference betwee...