How do bitwise operations work in Python?

2024/10/4 9:22:58

I have been learning about Bitwise operations today and I learned that Not (~) inverses all bits, e.g.:

01010
to
10101

which means ~10 should be -5 but instead I have seen that it is -11 (per the python command line) which is

01010
to
11011

only two of the bits have been inverted. Can anybody explain why it isn't 10101?

EDIT: After looking on my calculator I understand it a little better, But my own code for determining binary and ints is still being confused. Entering in (in byte mode) 11110101 gives me -11 but the same entered in my code gives -117:

def binaryToInt(biNum, bUnsigned = False):iNum = 0bSign = int(biNum[0]) if not (bUnsigned or biNum[-1] == "u") else 0biNum = biNum[(1 if not (bUnsigned or biNum[-1] == "u") else 0):(len(biNum) if biNum[-1] != "u" else -1)]for i in xrange(len(biNum)):iNum += int(biNum[i]) * 2**(len(biNum) - 1 - i)return (iNum if not bSign else -iNum)def intToBinary(iNum, bUnsigned = False):bSign = "1" if iNum < 0 else "0"iLoopNum = int((iNum ** 2) ** 0.5) #make positive!biNum = ""while iLoopNum:biNum += str(iLoopNum%2)iLoopNum /= 2return bSign + biNum[::-1] if not bUnsigned else biNum[::-1] + "u"

can one of you explain that?

Answer

Assuming that values are 32 bits, 10 is

00000000000000000000000000001010

and if you invert all those bits, you get

11111111111111111111111111110101

or -11. Because it's a 2's complement system!

https://en.xdnf.cn/q/70627.html

Related Q&A

How to split large wikipedia dump .xml.bz2 files in Python?

I am trying to build a offline wiktionary using the wikimedia dump files (.xml.bz2) using Python. I started with this article as the guide. It involves a number of languages, I wanted to combine all th…

CherryPy interferes with Twisted shutting down on Windows

Ive got an application that runs Twisted by starting the reactor with reactor.run() in my main thread after starting some other threads, including the CherryPy web server. Heres a program that shuts d…

From subprocess.Popen to multiprocessing

I got a function that invokes a process using subprocess.Popen in the following way:def func():...process = subprocess.Popen(substr, shell=True, stdout=subprocess.PIPE)timeout = {"value": Fal…

Assigning float as a dictionary key changes its precision (Python)

I have a list of floats (actually its a pandas Series object, if it changes anything) which looks like this:mySeries:... 22 16.0 23 14.0 24 12.0 25 10.0 26 3.1 ...(So elements…

Installing jpype in Mountain Lion

I am trying to install jpype in Mountain Lion. I followed all the steps suggested in this post: How to install JPype on OS X Lion to use with Neo4j?However, there is a glitch with Mountain Lion. I hav…

Most efficient way to index words in a document?

This came up in another question but I figured it is best to ask this as a separate question. Give a large list of sentences (order of 100 thousands):[ "This is sentence 1 as an example", &qu…

python libclang bindings on Windows fail to initialize a translation unit from sublime text

Short description: using libclang to autocomplete code does not work with python that comes bundled with Sublime Text 3.Details: A small verifiable example is in the repo on GithubIn essence, there is …

How to create a simple Gradient Descent algorithm

Im studying simple machine learning algorithms, beginning with a simple gradient descent, but Ive got some trouble trying to implement it in python. Here is the example Im trying to reproduce, Ive got …

login_required decorator on a class based view in django

I have a working class based view. But when adding @login_required I get the error:AttributeError: function object has no attribute as_viewSomething is happening to the ResultListView here:from django.…

Generic way to get primary key from declaratively defined instance in SQLAlchemy

Does SQLAlchemy offer a generic way to get the primary key from a declaratively defined instance, so that if:Base = declarative_base()class MyClass(Base):__tablename__ = mytablekey = Column(Integer, pr…