Octalとは?Base-8 Number Systemを解説
Octalはcomputingで実用性の高いnumber systemの1つです。Unix file permissions、programming language literals、binary bitsのgroupを人間がcompactに読むための表現として使われます。
目次
Octalとは?
Octalはbase-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です。
| Position | Power of 8 | Decimal Value |
|---|---|---|
| Position 0, rightmost | 8^0 | 1 |
| Position 1 | 8^1 | 8 |
| Position 2 | 8^2 | 64 |
| Position 3 | 8^3 | 512 |
| Position 4 | 8^4 | 4096 |
そのため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つを表します。
| Octal | Binary | Decimal |
|---|---|---|
| 0 | 000 | 0 |
| 1 | 001 | 1 |
| 2 | 010 | 2 |
| 3 | 011 | 3 |
| 4 | 100 | 4 |
| 5 | 101 | 5 |
| 6 | 110 | 6 |
| 7 | 111 | 7 |
つまり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 1264Octal、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 octalOctal 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 -> 0o643Unix File PermissionsでのOctal
UnixとLinux file permissionsは、octalが特に力を発揮する場所です。permissionsは3 bitsずつの3 groupsとして保存されます。owner用、group用、everyone else用のgroupです。
| Permission | Symbol | Binary Bit | Octal Value |
|---|---|---|---|
| Read | r | 100 | 4 |
| Write | w | 010 | 2 |
| Execute | x | 001 | 1 |
各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は次の通りです。
| Octal | Symbolic | Typical Use |
|---|---|---|
755 | rwxr-xr-x | executables、scripts、directories |
644 | rw-r--r-- | regular filesとconfig files |
600 | rw------- | SSH keysなどのprivate files |
777 | rwxrwxrwx | everyoneにfull access。productionでは通常避けます |
400 | r-------- | 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 patternPython
# 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 10C/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化が異なります。
| Feature | Octal (Base-8) | Hexadecimal (Base-16) |
|---|---|---|
| Digits used | 0-7 | 0-9 and A-F |
| Bits per digit | 3 | 4 |
| Best for | permissionsと3-bit groups | bytes、colors、memory addresses |
| 1 byte, 8 bits | paddingまたはoverflowを含む3 digits | 2 digitsでぴったり |
| Modern usage | Unix 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を試す参考資料
- Ritchie, D.M. & Thompson, K. (1974). The UNIX Time-Sharing System. Communications of the ACM, 17(7). https://dsf.berkeley.edu/cs262/unix.pdf
- IEEE & The Open Group. chmod - change the file modes. POSIX.1-2017. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/chmod.html
- Mozilla Developer Network. parseInt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
- Python Software Foundation. Built-in Functions: oct(). https://docs.python.org/3/library/functions.html#oct
- Knuth, D.E. (1997). The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Addison-Wesley Professional.