新人Y子のプログラミングのお部屋17

2進数って重要なの?

[speech_bubble type="std" subtype="R1" icon="sanoyu-hatena.png" name="新人Y子"]そもそも、2進数って使うのですか?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]開発する分野にもよります。
分野によっては、2進数を意識せずにプログラミングできます。
ただ、特にIoTなど組込系においては2進数の使用頻度は少なくありません。
このため、理解しておくことをお勧めします。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-bikkuri.png" name="新人Y子"]IoTに興味があるので、2進数をもっと詳しく教えてください!
[/speech_bubble]

2進数を理解しよう

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]ではまず、2進数の性質を知るため、0から1づつ足していってみましょう。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]char型(8ビット整数型)で、0は、「00000000」です。
では、次の数の1は、どうなるでしょう?
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]「00000001」ですか?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]はい。その通りです。
それでは、その次の2は何になるでしょう?
先ほどの「00000001」に、1を足してください。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-hatena.png" name="新人Y子"]1+1=2 ですよね。でも、2進数に「2」という記号はないのですよね。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]そうですね。このため、桁は繰り上がります。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-hatena.png" name="新人Y子"]では、えーっと「00000010」ですか?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]正解です!
同様に10まで、1づつ足していってください。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]3は、「00000011」
4は、「00000100」(2桁目繰り上がり)
5は、「00000101」
6は、「00000110」
7は、「00000111」
8は、「00001000」(3桁目繰り上がり)
9は、「00001001」
10は、「00001010」
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]正解です。
2進数は繰り上がりが早いので、10進数では1桁の数(0~9)を表すのに、2進数では4桁になっていることが分かると思います。
さて、次にマイナス値の表現について見てみましょう。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]はい。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]コンピュータの場合、符号もビットで表現するんです。
一番左(最上位)のビットが符号ビットです。符号ビットが「1」になっているものがマイナス値になります。

例)char型の符号ビット

[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]という事は、例えば、-1は、「10000001」になるのでしょうか?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]惜しい!
一般的なコンピュータは、そう表現しないんです。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]なぜでしょう?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]理由は、ゼロの状態が、ゼロと、マイナスゼロの2つできてしまい、何かと都合がよくないんです。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]確かにそうですね。
では、どうやって表現するのでしょう?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]「2の補数」という表現をします。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-hatena.png" name="新人Y子"]2の補数?
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="techtrageman-normal.png" name="テクトレージマン"]次回に続く[/speech_bubble]

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

目次