Numeral Systems
Reading material
- https://en.wikipedia.org/wiki/Numeral_system
- https://www.britannica.com/science/numeral/Development-of-modern-numerals-and-numeral-systems
Decimal
int decimalInt = 15;
long decimalLong = 400L;
Converting other number systems to Decimal
Starting from the right, by the corresponding power of the base number, which is 2 for binary numbers, 8 for octal numbers, and so on.
To convert octal numbers to the decimal system:
(digit0 x 8^0) + (digit1 x 8^1) + (digit2 x 8^2) + ... + (digitn x 8^n)
Based on this formula, the octal number 0273 converts to 187: starting from the right, the digit 3 is multiplied with 8 to the power of 0, then the digit 7 is multiplied with 8 to the power of 1, and so on.
(2 x 8^2) + (7 x 8^1) + (3 x 8^0)
= 128 + 56 + 3
= 187
A similar concept applies to hexadecimal numbers, using the base of 16 for the calculation. For example, the hexadecimal number 0xABC1 can be converted to 43969 as follows:
(A x 16^3) + (B x 16^2) + (C x 16^1) + (1 x 16^0)
= 40960 + 2816 + 192 + 1
= 43969
In Java
String binaryNumber = "1010";
int decimalFromBinary = Integer.parseInt(binaryNumber, 2);
String octalNumber = "10";
int decimalFromOctal = Integer.parseInt(octalNumber, 8);
String hexNumber = "A";
int decimalFromHex = Integer.parseInt(hexNumber, 16);
Converting Decimal to other number systems
In Java
int decimalNumber = 165;
String binaryNumber = Integer.toBinaryString(decimalNumber); //10100101
String octalNumber = Integer.toOctalString(decimalNumber); //245
String hexNumber = Integer.toHexString(decimalNumber); //a5
Binary
Binary numbers are also useful for bitwise logical operations, such as AND, OR, and NOT. These operations can be performed quickly and efficiently in a computer’s processor using simple digital logic circuits.
To declare a binary number in Java, you can use the prefix 0b or 0B followed by the binary digits. For example:
int binaryInt = 0b1010;
long binaryLong = 0B1010L;
Octal
base-8 system
uses eight digits (0-7) to represent numbers
In Unix-based operating systems, for instance, file permissions are represented using octal numbers. This means that, when working with file permissions in Java, you may need to use octal numbers to set the permissions of a file.
To declare an octal number in Java, you use the digit 0 as a prefix, followed by the octal digits. For example:
int octalInt = 0273;
long octalLong = 0273L;
Hexadecimal
base-16 system
uses a total of 16 characters, that is ten digits (0-9) and six letters (A-F), to represent numbers. The letters represents the numbers 10 to 15.
The advantages of using the hexadecimal system are apparent whenever you need a more terse and concise way of representing numbers – for instance when dealing with low-level programming tasks.
Computer memory addresses and color codes are indeed often represented with hexadecimal numbers, as the corresponding binary representations would be considerably harder to read for humans.
To declare a hexadecimal number in Java, you use 0x or 0X as a prefix, followed by the digits. For example:
int hexInt = 0x10e;
long hexLong = 0XABC1;
Roman numerals
https://en.wikipedia.org/wiki/Roman_numerals
Latin Alphabet | Decimal Value |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Conversion between Number Systems
When working with low-level programming, we might need to convert between decimal and binary or hexadecimal numbers to be able to set configurations.