0 votes
1 view
in Python by (3.3k points)

In C I could, for example, zero out bit #10 in a 32-bit unsigned value like so:

unsigned long value = 0xdeadbeef;
value &= ~(1<<10);

How do I do that in Python?

1 Answer

0 votes
by (6.8k points)

For manipulating the bits, there is a special kind of sets of  bitwise operators in Python like

 

OPERATOR

DESCRIPTION

SYNTAX

&

Bitwise AND

x & y

|

Bitwise OR

x | y

~

Bitwise NOT

~x

^

Bitwise XOR

x ^ y

>> 

Bitwise right shift

x>>

<< 

Bitwise left shift

x<<

Consider the below example :

To perform left shift in python be careful as Python integers aren't fixed-width so we have to use bit masks to obtain the low order bits.

(B << 5) & 0xffffffff

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...