There is a code in C:

unsigned long result = (18lu << 340lu); printf("result = %lu\n", result); 

Result of performance:

 result = 0 

However, the code is:

 unsigned long a = 18; unsigned long b = 340; unsigned long result = (a << b); printf("result = %lu\n", result); 

It gives the result:

 result = 18874368 

Attempt to implement this Python code:

 a = c_uint32(18) b = c_uint32(340) result = c_uint32(a.value << b.value) print("result =", result) 

Gave, respectively:

 result = c_ulong(0) 

Therefore, the direct question is: how does the left shift in C work with variables and how can one implement the same behavior in Python preserving the dimension c_uint32, and not the standard integer?

    2 answers 2

    In C ++, a big shift - UB (I do not see a solid link about C)

    If you are on the left side of the box?

    Intel's assembler shifts modulo the size of the number in bits (32, 64).

    Accordingly, by analogy in Python, you can do

     result = c_uint32(a.value << (b.value % 32)) 

      Open the cppreference :

      If you’re on the right?

      That is, when the size of the type is N bits, you can only shift by 0 <= x < N bits, otherwise you get undefined behavior.

      how can i implement the same behavior in python

      Since the behavior is not defined, the answer - no way.