WGC1 第2章数値表現 2.2記数法

WGC1 第1章 1.4グレートコードを特徴付けものの続き


2.2.2 基数

基数は基数点の左に置かれた各数に段階的に適用しいていく累乗値
少数点という用語は、10進数だけに用います。

  1  2  3 .  4  5
  10^2
     10^1
        10^0
            10^-1
                10^-2

少数点という用語は、10進数だけだったか。ふむ。


2.2.3.2 2進数を読みやすくする表現法

一般に、プログラム内でリテラルの2進数定数を使用できるのは、
アセンブリ言語コンパイラ(アセンブラ)内だけです。
監訳者注
Rubyは2進数定数を表現できます。Rubyでは、0bで始まる数値表現が2進数表現で
あるとみなされ、見やすいように_を区切りのために入れることもできます。
たとえば「0b1010_1111_1101_0010」のように記述できます。


C言語での2進数表現を調べてみたら、gcc 4.4.0から0bから始まる2進数表現はできるようだ。
6.59 Binary constants using the `0b' prefix


実験

ソース
 #include <stdio.h>

 int main(int argc, char* argv[]){
     printf("%d\n", 0b0001);
     printf("%d\n", 0b0011);
     printf("%d\n", 0b0101);
     printf("%d\n", 0b1000);
     return 0;
 }
gcc 4.4.0
  • 1. バージョンの確認
    [oc@centos5 2.2.3_The_Binary_Numbering_System]$ gcc44 --version
    gcc44 (GCC) 4.4.0 20090514 (Red Hat 4.4.0-6)
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

   成功!!

    [oc@centos5 2.2.3_The_Binary_Numbering_System]$ gcc44 The_Binary_Numbering_System.c
  • 3. 実行
    [oc@centos5 2.2.3_The_Binary_Numbering_System]$ ./The_Binary_Numbering_System
    1
    3
    5
    8
gcc 4.1.2
  • 1. バージョンの確認
    [oc@centos5 2.2.3_The_Binary_Numbering_System]$ gcc --version
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

   エラー

    [oc@centos5 2.2.3_The_Binary_Numbering_System]$ gcc The_Binary_Numbering_System.c
    The_Binary_Numbering_System.c:4:20: error: invalid suffix "b0001" on integer constant
    The_Binary_Numbering_System.c:5:20: error: invalid suffix "b0011" on integer constant
    The_Binary_Numbering_System.c:6:20: error: invalid suffix "b0101" on integer constant
    The_Binary_Numbering_System.c:7:20: error: invalid suffix "b1000" on integer constant
Visual C++ 2008
  • 1. バージョンの確認
    Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.

   エラー

    D:\workspace\C_SandBox\10BOOKs\WGC1\2.2.3_The_Binary_Numbering_System>cl The_Binary_Numbering_System.c
    Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.

    The_Binary_Numbering_System.c
    The_Binary_Numbering_System.c(4) : error C2059: 構文エラー : 'サフィックスが無効です。'
    The_Binary_Numbering_System.c(4) : error C2146: 構文エラー : ')' が、識別子 'b0001' の前に必要です。
    The_Binary_Numbering_System.c(4) : error C2059: 構文エラー : ')'
    The_Binary_Numbering_System.c(5) : error C2059: 構文エラー : 'サフィックスが無効です。'
    The_Binary_Numbering_System.c(5) : error C2146: 構文エラー : ')' が、識別子 'b0011' の前に必要です。
    The_Binary_Numbering_System.c(5) : error C2059: 構文エラー : ')'
    The_Binary_Numbering_System.c(6) : error C2059: 構文エラー : 'サフィックスが無効です。'
    The_Binary_Numbering_System.c(6) : error C2146: 構文エラー : ')' が、識別子 'b0101' の前に必要です。
    The_Binary_Numbering_System.c(6) : error C2059: 構文エラー : ')'
    The_Binary_Numbering_System.c(7) : error C2059: 構文エラー : 'サフィックスが無効です。'
    The_Binary_Numbering_System.c(7) : error C2146: 構文エラー : ')' が、識別子 'b1000' の前に必要です。
    The_Binary_Numbering_System.c(7) : error C2059: 構文エラー : ')'

2.2.4.2 16進数表現と2進数の表現の変換

2進数 16進数
%0000 $0
%0001 $1
%0010 $2
%0011 $3
%0100 $4
%0101 $5
%0110 $6
%0111 $7
%1000 $8
%1001 $9
%1010 $A
%1011 $B
%1100 $C
%1101 $D
%1110 $E
%1111 $F

HLA?馴染みがないな。
2進数は%、16進数が$?
16進は0xの方が馴染みがあるな。

2進数 10110010101を16進数
1)左に0ビットを2づつ加えて長さを12ビットにする

    001011001010

2)4ビットごとに区切る

    0010_1100_1010

3)変換表で置き換える

    $2CA

表記のまとめ

言語 2進数 16進数 8進数
C言語 0b00010011(使えるものもある) 0xdead 0123
HLA %0001_0011 $DEAD -










Randall Hyde、鵜飼 文敏、まつもと ゆきひろ、後藤 正徳、トップスタジオ