Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (4k 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 (8.7k 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

 

Related questions

0 votes
1 answer
0 votes
0 answers
asked Feb 18, 2021 by Harsh (1.5k points)
0 votes
1 answer
asked Mar 14, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...