Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2140
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T16:08:03+00:00 2022-05-31T16:08:03+00:00

How do I do a bitwise Not operation in Python?

  • 0

[ad_1]

Python bitwise ~ operator invert all bits of integer but we can’t see native result because all integers in Python has signed representation.

Indirectly we can examine that:

>>> a = 65
>>> a ^ ~a
-1

Or the same:

>>> a + ~a
-1

Ther result -1 means all bits are set. But the minus sign ahead don’t allow us to directly examine this fact:

>>> bin(-1)
'-0b1'

The solution is simple: we must use unsigned integers.
First way is to import numpy or ctypes modules wich both support unsigned integers. But numpy more simplest using than ctypes (at least for me):

import numpy as np
a = np.uint8(0b1100)
y = ~x

Check result:

>>> bin(x)
'0b1100'
>>> bin(y)
'0b11110011'

And finally check:

>>> x + y
255

Unsigned integer ‘255’ for 8-bits integers (bytes) mean the same as ‘-1’ becouse has all bits set to 1. Make sure:

>>> np.uint8(-1)
255

And another simplest solution, not quite right, but if you want to include additional modules, you can invert all bits with XOR operation, where second argument has all bits are set to 1:

a = 0b1100
b = a ^ 0xFF

This operation will also drop most significant bit of signed integer and we can see result like this:

>>> print('{:>08b}'.format(a))
00001100
>>> print('{:>08b}'.format(b))
11110011

Finally solution contains one more operation and therefore is not optimal:

>>> b = ~a & 0xFF
>>> print('{:>08b}'.format(b))
11110011

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5379 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1169 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1111 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.