Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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

Computers store all kinds of information as a stream of binary digits called bits. Whether you’re working with text, images, or videos, they all boil down to ones and zeros. Python’s bitwise operators let you manipulate those individual bits of data at the most granular level.

Python isolates you from the underlying bits with high-level abstractions. You’re more likely to find the overloaded flavors of bitwise operators in practice. But when you work with them in their original form, you’ll be surprised by their quirks!

In this course, you’ll learn 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

Download

Sample Code (.zip)

13.4 KB

Download

Course Slides (.pdf)

3.6 MB

00:00 Welcome to Binary, Bytes, and Bitwise Operators in Python. My name is Chris, and I will be your guide.

00:07 This course introduces you to binary number concepts and how you can use them in the Python programming language. In particular, you’ll learn about binary numbers, bitwise math and truth tables, number representations, fixed- and arbitrary-precision integers in Python, bitwise operators in Python, frequent uses of binary and bitwise operations, big-endian and little-endian byte orders, and overloading bitwise operators. A quick note about versions.

00:40 The code in this course was tested using Python 3.9. Nothing is very version-specific, but f-strings are used in some places. f-strings were introduced in Python 3.6, so to follow the code along exactly, you’ll need at least Python 3.6.

00:58 Everything is digital these days. At the time of this recording, I’m shopping for a new washing machine. It’s pretty much impossible to find one with a mechanical control anymore.

01:07 The retailers keep telling me how great it is that it can talk to my phone via Bluetooth. Until my phone figures out how to do my laundry, I’m not sure why this is important, but it does underline the fact that computer processors are everywhere.

01:21 CPUs in computers are built on transistors, which are tiny components that act like switches. Every piece of data in your computer is represented through a series of these switches, with the patterns of on and off forming a binary number.

01:36 The 1s and 0s in the binary number correspond to the on and off states of these switches. These binary numbers are then mapped to represent your data, such as going through a numeric transform to translate binary to decimal, doing a code lookup to translate binary to a character, or directly influencing the state of hardware in your system. Python isn’t the easiest choice for manipulating raw binary data.

02:04 It has lots of tools that can help you out, but some of the high-level language features that make it easier to code other things can make dealing with binary a bit more of a challenge.

02:15 In grade school, you learned simple math operations like addition and multiplication. With binary data, you can do these operations, as well as some others.

02:23 There’s a family of binary operators that are called bitwise operations. Each of these operations either combines two binary values into a third or manipulates the bits in a binary value.

02:36 Most of the operations can be thought of as a series of steps performed on the bits within the binary number. The first bitwise operator I’m going to talk about is AND.

02:46 AND combines two binary numbers, comparing the corresponding bits in each. If the corresponding bits are both 1 in the numbers being combined, then the bit is 1 in the result.

02:58 OR also combines two binary numbers. This time, the bits in the resulting number are 1 if the corresponding bits are 1 in either of the operands.

03:09 XOR is short for “exclusive or.” It is like OR, but the bits are on only if the bit is on in exactly one of the operands. If both operands have a bit value of 1, the result will have a value of 0.

03:24 Everything up until here has combined two operands. The next three operate on a single value. NOT, also known as negation or inversion, flips the state of all the bits in the number. All the 1s become 0 and vice versa.

03:41 This is the left shift. All the bits in the result are the operand’s value shifted to the left by n positions. The new bits pushed in on the right-hand side have a value of 0. Corresponding to the left shift is a right shift, moving the bits to the right instead of the left.

04:02 Using bitwise operators, you can create and manipulate flags, access a portion of a number, or manipulate raw binary data.

04:14 Next up, I’ll dive into binary numbers. The 0s aren’t so bad, but if you land on a 1, it’s a bit pointy. Thanks. Tip your waitress! I’ll be here all week.

Become a Member to join the conversation.