Published Date : 2021年6月14日19:29

002 Pythonでビットコインを学ぶ (文字コードとバイナリとビットシフト)
002 Use python to learn bitcoin (Character Codes, Binaries, and Bit shifts)


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



文字コードとバイナリとビットシフト

Character Codes, Binaries, and Bit shifts

前回の動画で使われていた文字コードとバイナリをPythonを使ってもう少し詳しく操作してみましょう。

Let's operate at the character codes and binaries used in my previous video in more detail using python.

まず文字を十進法の文字コード、二進数、十六進数に変換してみましょう。

First, Let's convert the characters to decimal character code, binary, and hexadecimal.

In []: print(ord('a'))
97

In []: print(bin(ord('a')))
0b1100001

二進数を十進数に変換する計算方法は、一桁目は2のゼロ乗、二桁目は2の1乗といった具合に最後の桁まで計算して、それらを足し合わせていくだけです。

To convert a binary number to a decimal number, you simply calculate the first digit to two to the power of zero, the second digit to two to the power of one, and so on up to the last digit, and add them together.

一桁目を一ビットと言います、そして一ビット目が取る値はゼロか一、二ビット目が取る値はゼロか二、三ビット目が取る値はゼロか四と続きます。

The first digit is called one bit, and the value taken by the first bit is zero or one, the value taken by the second bit is zero or two, the value taken by the third bit is zero or four, and so on.

このように二進数は一桁目が一の位、二桁目が二の位、三桁目が四の位とn桁目が二のn-1乗の位になります。

Thus, in binary, the first digit is the number one, the second digit is the number two, the third digit is the number four, and the nth digit is the order of two to the power of n-1.

十六進数も考え方は同じです。一桁目は0から15までの値で、二桁目は16から16掛ける15までの値、そしてn桁目が16のn-1乗の位になります。

Hexadecimal numbers have the same idea. The first digit is a value from 0 to 15, the second digit is 16 to 16 times 15, and the nth digit is 16 to the power of n-1.

それでは文字列を一文字ずつ十進法の文字コード、二進数、十六進数に変換してみましょう。

Let's convert the string character by character to decimal character code, binary, and hexadecimal.

前の動画でbytesメソッドを使って文字列をutf-8にエンコードしました。それらも一つずつ確認していきましょう。

In my previous video, we encoded a string into utf-8 using the bytes method. Let's check them one by one.

ご覧の通りエンコードされた文字列の文字は十進法の文字コードになっています。

As you can see, the characters in the encoded string are decimal character codes.

ではこの文字コードを16進数や二進数に変換して、ビットシフトというものを行ってみましょう。

Now let's convert this character code to hexadecimal or binary and do a bit shift.

In []: for b in bytes("hello world", encoding="utf-8"):
            print(hex(b))

0x68
0x65
0x6c
0x6c
0x6f
0x20
0x77
0x6f
0x72
0x6c
0x64
In []: for b in bytes("hello world", encoding="utf-8"):
            print(hex(b >> 1))

0x34
0x32
0x36
0x36
0x37
0x10
0x3b
0x37
0x39
0x36
0x32

どうやら右シフトは元の値を二のn-1乗割り算をしているようです。

Apparently the right shift divides the original value by two to the power of n-1.

二進法にしてみればより分かりやすくなります。

Converting it to binary will help you understand.

このように、右シフトは最後の桁を無くして、桁数が削減されています。

As you can see, shifting to the right eliminates the last digit and reduces the number of digits.

なぜこうなるのかは10進法の数で考えると分かりやすいです。

The reason for this is easy to understand when you consider the number in decimal.

例えば1000を右に一つだけシフトして一桁目を消すとします。

For example, suppose you shift 1000 to the right to remove the first digit.

そうすると100になります。これは1000を10で割った数と同じです。

Then it's 100. This is equal to 1000 divided by 10.

つまり、1000を右にn回シフトすると、1000を10進法のベースとなる10のn-1乗で割ることになるのです。

That is, shifting 1000 to the right n times divides 1000 by the base 10 of the decimal number system raised to the power n-1.

これを二進数にあてはめると、数字の1000は8、1000を右一回シフトすると100となり4になります。

If you apply this to a binary number, the number 1000 becomes 8, and if you shift 1000 right once, it becomes 100, which is 4.

つまり、1000(二進数で8を示す)を右にn回シフトすると、8を2進法のベースとなる2のn-1乗で割ることになるのです。

This means that shifting 1000 (denote 8 in binary) to the right n times will divide 8 by the base 2 of the binary system raised to the power n-1.

左シフトも考え方は同じです。つまり二進数のxに左シフトをn回行うということはxに2のn-1乗掛けているのです。

The concept of left shift is the same. In other words, shifting x to the left n times is equivalent to multiplying x by 2 raised to the power n-1.

16進数のビットシフトも同様です。因みに、何故16進数が使われているかというと、単純に人間にとって読みやすいからです。

The same applies to the bit shifts in hexadecimal. Note that the hexadecimal number is used because it is simply that they are readable by humans.

二進数の場合だと桁数が大きくなったり、二進数の数字が沢山並んでいると、みな同じように見えて訳が分からなくなってしまいます。

In the case of binary numbers, if the number of digits is large or if there are many binary numbers are displayed, they all look the same and are hard to understand.

なので、二進表記より短く表すことができる16進数が使われるようになったわけです。

So, hexadecimal numbers, which can be expressed shorter than binary, are now used.

今度は16進数や二進数を整数の値で表示してみましょう。

Now let's display hexadecimal or binary numbers as integer values.

intメソッドの第二引数に二進数なら数字の2を、16進数なら16と指定します。

The second argument to the int method is specified the number 2 for binary, 16 for hexadecimal.

先ほどのビットシフトをこのintメソッドを使って確認してみましょう。

Let's use this int method to verify the bit shifts.

今度は300までの文字コードを表示させてみましょう。

Now let's display the character codes up to 300.

表示できていない箇所は制御文字と言われるもので、例えばDELETEキーを指すDEL等があります。

The parts that cannot be displayed are called control characters, for example DEL which indicates DELETE key.

この制御文字を除いて文字を表示させたい場合はunicodedataモジュールを使うと便利です。

The unicodedata module is useful if you want to display characters other than these control characters.

どうやら制御文字はCcといった記号でカテゴライズされているようです。

It seems that control characters are categorized by symbols such as Cc.

任意の文字コードを選んでビットシフトを試してみましょう。

Try bit shifting with any character code.



以上です。お疲れ様です。

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