1
00:00:00,000 --> 00:00:04,990
Python also has a full set of bitwise operators for operating on binary values,

2
00:00:05,000 --> 00:00:07,990
and occasionally this comes in handy.

3
00:00:08,000 --> 00:00:13,990
It's a little bit difficult to show, because when you print a value, for

4
00:00:14,000 --> 00:00:18,990
example if I just say 5 here, it will print it in decimal.

5
00:00:19,000 --> 00:00:21,990
Even if I specify it in binary,

6
00:00:22,000 --> 00:00:28,990
if I say it 0b0101, which is a five, it still prints it in decimal.

7
00:00:29,000 --> 00:00:34,990
So I am going to write a little one-line function, and I'll call it b for

8
00:00:35,000 --> 00:00:36,990
printing binary values.

9
00:00:37,000 --> 00:00:46,990
I am going to use a format string here, and we'll learn more about those in a later chapter.

10
00:00:47,000 --> 00:00:57,990
The format method of the string, like that, and now when I say b5 like that, it

11
00:00:58,000 --> 00:01:01,990
prints the binary value to 8 places.

12
00:01:02,000 --> 00:01:05,990
That's basically what that format string does inside the curly braces,

13
00:01:06,000 --> 00:01:11,990
the :08b, that prints out a binary value to 8 places.

14
00:01:12,000 --> 00:01:16,990
So now we can look at the binary operators, and we can see what they do on a bitwise level.

15
00:01:17,000 --> 00:01:23,990
So, we are going to define a couple of values, and we will call them x and y. And

16
00:01:24,000 --> 00:01:29,990
x, we'll give it a couple of fives, so that it's 010101 et cetera.

17
00:01:30,000 --> 00:01:33,990
We will do that like this.

18
00:01:34,000 --> 00:01:39,990
And y, we'll do the same thing, but with the opposite bits and that happens to

19
00:01:40,000 --> 00:01:42,990
be xaa in hexadecimal.

20
00:01:43,000 --> 00:01:49,990
So if we look at these values, x looks like that and y looks like that.

21
00:01:50,000 --> 00:01:57,990
So the or of these two values should be x|y, and the vertical bar there is the

22
00:01:58,000 --> 00:02:00,990
or operator.

23
00:02:01,000 --> 00:02:06,990
We will have all the bits set, like that, and the and of the two values, which is

24
00:02:07,000 --> 00:02:12,990
with the ampersand, will have all the bits cleared.

25
00:02:13,000 --> 00:02:19,990
The exclusive or will look just like the or.

26
00:02:20,000 --> 00:02:31,990
So instead, let's take the exclusive or of, say, x and, say, 0 and that will look like that.

27
00:02:32,000 --> 00:02:38,990
Or if we take the exclusive or of x and all the bits on, it will look like

28
00:02:39,000 --> 00:02:40,990
that, because all of the bits get flipped.

29
00:02:41,000 --> 00:02:47,990
We also have shift operators. We can say x shifted left by four bits, and

30
00:02:48,000 --> 00:02:53,990
there we have four clear bits on the right, or we can say x shifted to the right

31
00:02:54,000 --> 00:02:58,990
by four bits, and it clears all the bits to the left.

32
00:02:59,000 --> 00:03:03,990
We have the one's compliment operator which is the unary operator and it just

33
00:03:04,000 --> 00:03:06,990
looks like that, a little tilde.

34
00:03:07,000 --> 00:03:11,990
Since it operates on the word size of the implementation, which is more than

35
00:03:12,000 --> 00:03:16,990
the 8 bits that I am printing, it gets a sign extension, and it ends up looking like that.

36
00:03:17,000 --> 00:03:20,990
So those are the bitwise or binary operators.

37
00:03:21,000 --> 00:03:23,990
The word binary could mean having to do with two values.

38
00:03:24,000 --> 00:03:25,990
So we call them bitwise operators.

39
00:03:26,000 --> 00:03:36,000
Those are the bitwise operators available in Python.

