/7分で読めます

Octalとは?Base-8 Number Systemを解説

Octalはcomputingで実用性の高いnumber systemの1つです。Unix file permissions、programming language literals、binary bitsのgroupを人間がcompactに読むための表現として使われます。

Octalとは?

Octalbase-8とも呼ばれ、0から7までの8 digitsだけを使うpositional number systemです。decimalと似ていますが、各placeは10のpowersではなく8のpowersに基づきます。

computingでは、octal valuesはdecimal numbersと混同しないように0o prefix付きで書かれることがよくあります。たとえばoctalの0o17はdecimalの15です。

Octalが便利なのは、octal digit 1つがちょうど3 binary bitsに対応するためです。このきれいな関係により、early computingでoctalは人気になり、現在もUnix file permissionsで重要です。

Octalの仕組み

どのpositional number systemでも、各digitのvalueはpositionで決まります。octalではpositionsは8のpowersです。

PositionPower of 8Decimal Value
Position 0, rightmost8^01
Position 18^18
Position 28^264
Position 38^3512
Position 48^44096

そのためoctal number 375は次の意味になります。

  3 x 8^2 = 3 x 64 = 192
+ 7 x 8^1 = 7 x 8  =  56
+ 5 x 8^0 = 5 x 1  =   5
                     -----
                       253  (decimal)

OctalとBinaryの関係

octalがcomputingで便利な理由は単純です。8 = 2^3なので、octal digit 1つが3 binary bitsのgroup 1つを表します。

OctalBinaryDecimal
00000
10011
20102
30113
41004
51015
61106
71117

つまりoctalとbinaryはdigitごとに変換できます。長い計算は不要です。

Octal:   7   5   3
Binary: 111 101 011

So octal 753 = binary 111101011

And going back:
Binary: 1 010 110 100
Octal:  1   2   6   4

So binary 1010110100 = octal 1264

Octal、Decimal、Binary間の変換

Octal to Decimal

各digitに8のpowerを掛け、結果を足し合わせます。

Octal 755:
  7 x 8^2 = 7 x 64 = 448
  5 x 8^1 = 5 x 8  =  40
  5 x 8^0 = 5 x 1  =   5
                    -----
                      493  (decimal)

Decimal to Octal

8で繰り返し割り、remaindersを集めます。remaindersを下から上へ読みます。

Convert 493 to octal:
  493 / 8 = 61 remainder 5
   61 / 8 =  7 remainder 5
    7 / 8 =  0 remainder 7

Read the remainders upward: 755
493 decimal = 755 octal

Octal and Binary

各octal digitを3-bit binary groupへ置き換えるか、binary digitsを右から3つずつgroup化します。

Octal to Binary:
  0o755 -> 7=111  5=101  5=101 -> 111 101 101

Binary to Octal:
  110100011 -> group by 3 from right: 110 100 011
                                       6   4   3 -> 0o643

Unix File PermissionsでのOctal

UnixとLinux file permissionsは、octalが特に力を発揮する場所です。permissionsは3 bitsずつの3 groupsとして保存されます。owner用、group用、everyone else用のgroupです。

PermissionSymbolBinary BitOctal Value
Readr1004
Writew0102
Executex0011

各permission groupがちょうど3 bitsなので、octal digit 1つがpermission group全体を表します。classicなchmod 755は次のように分解できます。

chmod 755 file.sh

  7 = 4+2+1 = rwx  -> Owner:  read + write + execute
  5 = 4+0+1 = r-x  -> Group:  read + execute
  5 = 4+0+1 = r-x  -> Others: read + execute

Binary: 111 101 101
Symbol: rwx r-x r-x

よく見かけるpermission patternsは次の通りです。

OctalSymbolicTypical Use
755rwxr-xr-xexecutables、scripts、directories
644rw-r--r--regular filesとconfig files
600rw-------SSH keysなどのprivate files
777rwxrwxrwxeveryoneにfull access。productionでは通常避けます
400r--------ownerのみread-only、deployed secrets

Programming LanguagesでのOctal

多くのprogramming languagesはoctal literalsをsupportしますが、syntaxは異なります。

JavaScript

// Modern octal (ES6+) - recommended
const permissions = 0o755;   // 493 in decimal
const mask = 0o022;          // 18 in decimal

// Parse octal string to number
parseInt("755", 8);          // 493

// Convert decimal to octal string
(493).toString(8);           // "755"

// Legacy leading-zero octal is confusing
// const old = 0755;  // Avoid this pattern

Python

# Octal literal
permissions = 0o755       # 493 in decimal

# Convert decimal to octal string
oct(493)                  # '0o755'

# Parse octal string to int
int("755", 8)             # 493

# Set file permissions
import os
os.chmod("script.sh", 0o755)

C / C++

// Octal literals use a leading 0
int perms = 0755;    // 493 in decimal
int mask  = 022;     // 18 in decimal

// Print in octal
printf("%o\n", 493);  // Prints: 755

// Common bug:
int x = 010;  // This is 8, not 10

C/C++のclassic octal trap

CとC++では、0で始まるnumberはoctalとして解釈されます。そのため010は10ではなく8です。leading zeroesでnumberをpadすると、初心者を驚かせたりbugsの原因になったりします。

Octalの簡単な歴史

Octalには、多くのdevelopersが思うより長い歴史があります。

  • Before computers - 指そのものではなく指の間を数えるなど、一部のculturesではbase-8 counting systemsが使われました。
  • 1950s and 1960s - 12、18、24、36 bitsのようなword sizesを持つearly machinesでは、3-bit groupsにきれいに分けられるoctalが自然に合いました。
  • Unix era - Unixはowner、group、othersそれぞれに3 permission bitsを持つ9-bit permission modelを使いました。3 octal digitsはこのmodelを完璧に表しました。
  • Modern computing - 8-bit bytesが支配的になるにつれてhexadecimalがより一般的になりましたが、3-bit groupsが自然な場所ではoctalが残りました。

OctalとHexadecimal

Octalとhexadecimalはどちらもbinaryをcompactに書く方法ですが、bitsのgroup化が異なります。

FeatureOctal (Base-8)Hexadecimal (Base-16)
Digits used0-70-9 and A-F
Bits per digit34
Best forpermissionsと3-bit groupsbytes、colors、memory addresses
1 byte, 8 bitspaddingまたはoverflowを含む3 digits2 digitsでぴったり
Modern usageUnix permissionsとlegacy code多くのlow-level contextsで主流

general low-level computingでは、byteが8 bitsで2 hex digitsにぴったり収まるためhexadecimalが主流になりました。しかし3-bit groupings、特にUnix permissionsでは、octalが今でも自然で読みやすい選択です。

Octal Numbersをすぐに変換

無料のOctal Converter toolで、octal、decimal、binary、hexadecimalをbrowser内ですぐに相互変換できます。

Octal Converterを試す

参考資料

  1. Ritchie, D.M. & Thompson, K. (1974). The UNIX Time-Sharing System. Communications of the ACM, 17(7). https://dsf.berkeley.edu/cs262/unix.pdf
  2. IEEE & The Open Group. chmod - change the file modes. POSIX.1-2017. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/chmod.html
  3. Mozilla Developer Network. parseInt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
  4. Python Software Foundation. Built-in Functions: oct(). https://docs.python.org/3/library/functions.html#oct
  5. Knuth, D.E. (1997). The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Addison-Wesley Professional.