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

前回は、整数型の種類とそれぞれのビット数について学習しました。

整数型 器の大きさ(ビット数)
char 型  8ビット
short 型  16ビット
int 型  32ビット(*1)
long 型  64ビット(*1)

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]今回は、「char」型を例にとって、表現できる整数の範囲を見てみましょう。[/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ビットで表せる組み合わせは、「0」と「1」の2通りですが、8ビットで表せる組み合わせは何通りでしょう?[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]確か、1ビット増える毎に、組み合わせが倍になってましたよね。なので、8回倍々していきますね。
1ビットで2通り
2ビットで4通り
3ビットで8通り
4ビットで16通り
5ビットで32通り
6ビットで64通り
7ビットで128通り
8ビットで… 256通りですか?
[/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子”]8ビットなので、

ということですね。
[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]はい。そうです。
それでは、本題の、8ビット整数型「char」で表せる値の範囲を考えてみてください。
但し、本来整数はマイナスの値も含まれますが、まずは、マイナスを考慮しない、ゼロ以上の整数の範囲を考えてください。[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-hatena.png” name=”新人Y子”]ゼロから256通りの整数なので、、、
値の範囲は、0~255になりますか?[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]その通りです。
値の範囲は、0(ゼロ)から

になります。[/speech_bubble]

■10進数と2進数対応表(マイナスを表現しない場合)

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]では、マイナスは、どのように表現されるのでしょう?[/speech_bubble]

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

(*1) 処理系によって異なる場合があります

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

この記事を書いた人

目次