Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Binary, Bytes, and Bitwise Operators in Python (Summary)

Mastering Python bitwise operators gives you the ultimate freedom to manipulate binary data in your projects. You now know their syntax and different flavors as well as the data types that support them. You can also customize their behavior for your own needs.

You also learned how computers use the binary system to represent different kinds of digital information. You saw several popular ways to interpret bits and how to mitigate the lack of unsigned data types in Python as well as Python’s unique way of storing integer numbers in memory.

In this course, you learned how to:

  • Read binary numbers
  • Perform bitwise math and read truth tables
  • Represent fixed and arbitrary precision itegers in Python
  • Perform bitwise operations in Python
  • Use bitmasks to pack information on a single byte
  • Differentiate Big-Endian and Little-Endian byte orders
  • Overload Python bitwise operators in custom data types

For images used in this video course, credit goes to @FX13, @mondspeer, @Juhele, @GDJ, @BigRedSmile, @vectorace, @speciwoman, @Merlin2525, and @vermeil at Openclipart, as well as C2RMF at Wikimedia Commons and fgrieu at StackExchange.

Download

Sample Code (.zip)

13.4 KB
Download

Course Slides (.pdf)

3.6 MB

00:00 In the previous lesson, I demonstrated operator overloading and how to write your own implementations of the bitwise operators. In this lesson, the final lesson, I’ll summarize the course.

00:12 Binary is the heart of all CPUs, and lately, CPUs are at the heart of everything. When I was a kid, a thermostat was two pieces of metal glued together on a stick.

00:22 Now they have artificial intelligence and tend to run Russian botnets. Where was I? Right. Binary values are formed based on the concepts of base-2 numbers, where each digit represents a power of 2. Bitwise operators combine or modify one or more binary values. AND, OR, and XOR combine two values matching the 1s, selecting the 1s, or exclusively selecting the 1s. NOT operates on a single value, inverting all the bits.

00:55 You can also shift binary values left or right. Python only supports the arithmetic shift, which is conscious of the sign bit, but other languages support logical shifts as well, ignoring the sign bit.

01:10 Everything in your computer is a binary value, just mapped differently. Unsigned integers map directly to the powers of 2. Negative numbers need a different mapping, two’s complement being the most common. And floating-point and fixed numbers are packed sets of bits, portions of each, meaning different parts of the number they’re expressing.

01:34 Python is a little creaky when it comes to bitwise work because it doesn’t have an unsigned integer type. This isn’t a complete limitation. You can work around it, but you do have to be careful.

01:45 Python stores integers in two different internal formats: fixed and arbitrary precision. The first usually uses eight bytes and has a maximum value. The second has no maximum but isn’t native to your CPU.

01:59 As all the integers are signed, you need to be careful when dealing with bitwise operations, as it can create unexpected behaviors with negative numbers.

02:09 Most times when you’re interested in binary, you’re not going to be dealing with negative numbers—just the raw bits. Using a bitmask can help you enforce the use of a subset of bytes of your Python integer, getting around the negative number problem most of the time. You can use bitwise operations to implement sets of binary flags; get, set, and toggle individual bits in a value; and access portions of a value such as the RG and B parts of a color.

02:42 When bytes come together to make bigger numbers, the order they go in is different on different computer architectures. Big-endian puts the bytes in left-to-right order, whereas little-endian does right to left, like a stack of bytes.

02:56 Some processors are capable of working in either mode. These are called bi-endian. Finally, the last topic in the course was on overloading the bitwise operators in Python. Special methods called dunder for the win!

03:13 Please join me in thanking multiple people at Openclipart, Wikimedia Commons, and StackExchange for providing public domain and free-to-use diagrams. I’m grateful for their work, and so should you be. Without them, you’d be looking at scans of crayon drawings. Artistic, I am not. It’s been a long journey.

03:34 I’m glad you stuck with me through it all. I hope you found the course useful. Thanks for your attention.

Become a Member to join the conversation.