Published Date : 2021年6月15日18:09

003 Pythonでビットコインを学ぶ (ビットの論理演算)
003 Use python to learn bitcoin (logical operations on bits)

This blog has an English translation


This is a summary blog post about a video I uploaded to NicoNico.


Please refer to the video for details.


Table of Contents

① 動画の説明
① Video Description


Let's do logical operations on bits in Python


Last time we converted the character codes to decimal, binary, and hexadecimal, and then did a bit shift.


This time, let's do logical bit operations.


Let's start with logical operations on bits. There are six logical bit operations.


Think of bit 1 as the light bulb on and bit 0 as the light bulb off. That is easier to understand.


And imagine a row of light bulbs.


The first is the AND operation. This is 1 if the bits being compared are both 1.


The second is the OR operation. This is 1 if either of the bits being compared has 1.


The third is the NOT operation. This inverts the bit.


The fourth XOR operation. This is 1 only if one of the bits being compared is 1.


The fifth is NAND operation. This is 0 if the bits being compared are both 1, and 1 otherwise.


The sixth is the NOR operation. This is 0 if either of the bits being compared has 1. It becomes 1 when both are 0.


Let's reproduce the logical operation of bits in a Python script.

In [1]: pairs_bits = [(0,0),(0,1),(1,0),(1,1)]

In [2]: for b1,b2 in pairs_bits:
    ...:     print(f"{b1} & {b2} = {b1 & b2}")
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
In [3]: for b1,b2 in pairs_bits:
    ...:     print(f"{b1} or {b2} = {b1 | b2}")
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
In [4]: for b1,b2 in pairs_bits:
    ...:     print(f"{b1} xor {b2} = {b1 ^ b2}")
0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0
In [5]: for b1 in [0,1]:
    ...:     print(f"not {b1} = {~b1}")
not 0 = -1
not 1 = -2


Now, in the NOT operation, a strange thing happens.


The NOT operation for 0 is minus 1, and the NOT operation for 1 is minus 2.


I'll explain why this happens.


For example, for the 4 bits of 0000, all the digit combinations can be from 0000 to 1111 in 32 ways, representing decimal numbers 0 to 31.


However, in order to do negative representation with bits, rules are needed.


If you just want to display a number on your display, you can add a minus sign to the beginning of the number, but computers can only do things in 0000 (For example, 4 bits) with 32 patterns, so if the numbers 0 to 31 are already fixed, the computer doesn't have the concept of minus.


So, in order for the computer to be able to express a negative value, if there are 4 bits of 0000, it is decided to let the computer recognize up to 0111 as a positive number, 1000 to 1111 as a negative number, and the leftmost 1 as a flag indicating a negative value.


Then, when the number 1 is expressed by 4 bits, it becomes 0001, and when this is inverted by NOT operation, it becomes 1110 and becomes minus 2.


If you want to represent it as the inverse zero of 1, add 2 to that number.


The same is true for 0. In other words, it becomes 1111 and minus 1 when it is inverted. And if you add 2 to that number and make it 1, you get 1, which is the inverted bit of 0.


By combining these bit operations, it is possible to perform various calculations such as bit rotation from the four basic arithmetic operations and so on.


A NAND operation performs a NOT operation on an AND operation.


A NOR operation then performs a NOT operation on an OR operation.


Let's see what happens in the case of bits.


First, let's set the number of bits to 8 and calculate.


Enter the two bits you want to perform logical operations on with the Try and Except statement.


Let's turn each logical operation into a function for easier display later.


Let's create AND, OR, XOR, and NOT in that order.


A negative value appeared again at the NOT operation.


Let's work this out.


First, since the size of the digits of the bits is not the same between the two bits, these digits must be matched.


So, you specify a number of type int as the first argument and [b] as the second argument of the format function to convert it to a binary strings without [0b] at the beginning.


The len function is then used to determine the length of the binary character, which is the number of digits in the bits.


If the number of digits is the same or larger, the function returns the number of digits in the bits.


Again, enter the two bits you want to perform the logical operation on in the Try and Except statement.


This time, the number of digits of the two bits is correct.


Next, you create another custom function for the NOT operation instead of the standard Python NOT operation.


First, as a MASK, the bits to be inverted is shifted to the left by the number of digits of the bits and the digit is raised by one.


Subtract 1 from there and all bits will be 1.


Then, this MASK is used to perform an XOR operation on the bits you want to invert. This allows the NOT operation to be performed without negative values.


Let's create a new NOT function and see if it works.


You can then create NAND and NOR operations using custom functions for NOT operations.


Now let's use all of these functions together to see if they do a good logical operation on the specified bits.


Then let's do bit rotation next time.


That's all. Thank you for your hard work.