Use Case for Binary, Octal and HexaDecimal in Java
When you learned Java, you must have read about various numbering systems supported by JDK.For example when we append a single 0 before an int, it becomes octal and if 0x is appended then it becomes hexadecimal. Recently while programming, I have been thinking about real world scenario where we can use various numbering systems like octal, binary and hexadecimal apart from decimal. I came up with the following list and wanted to share:
Binary: The original number system for computers
- Binary numbers are everywhere used and this system is inherent to computers. There are dedicated data types for binary number system like BitSet, Byte arrays etc.
- Networking based routing programs use binary system for IP address calculations like finding the sub-net of a host, assigning gateway, routing a packet etc.
- From java 7 onwards, binary literals can be directly assigned to int variables by using something like 0b101010101.
HexaDecimal : The one that has replaces binary values almost everywhere
- 64-based encoding systems like Base64 which encode the binary data in hexadecimal are very good example where hexadecimal number system is used. Base64 is used
- Printer, Bar coders and other hardware interaction commands are usually hexadecimal codes. So programs having hardware interaction have to use this number system.
- For graphics based systems, the color codes are represented as hexadecimal values and hence again this number system can’t be avoided where colors are involved.
- Any java de-compiler will definitely need to handle hexadecimal values for conversion of byte code to source code.
Octal : The one loved by Unix
- Unix File permissions. Many java programs interact with Unix based systems where they need to send UNIX commands and have them executed by the operating system. The file permissions in Unix are usually three bits viz. Read, Write and Execute. Octal number system suits this requirement as octal also has three number positions.
- Some digital displays especially seven segment displays use the octal number system for creating mapping between which portion of display will be lit.
- Some ancient systems around year 1700 used 18bit word sizes which can nicely be represented using octal number system.