2

I’m reviewing the number systems and converting between them.

I thought hexadecimal values use less bits than binary, which makes sense but in the end aren’t the hex values stored as bits? Which seems to ultimately defeat the purpose.

chappie
  • 119
  • 4
  • 12
  • 1
    "Hexadecimal values use less bits than binary" – This makes no sense. Hexadecimal values don't use less bits than binary. They don't use any bits *at all*. They use hex. "Bit" *literally* means "binary digit" (which is of course a misnomer, since "digit" itself comes from the latin word for 10), so it only applies to binary. – Jörg W Mittag Sep 08 '19 at 15:06
  • An ASCII string of base-16 digits can represent the same number in fewer characters than an ASCII string of base-2 digits. But the original number itself is normally stored in binary. **Hex and decimal are just serialization formats for numbers**; a C `int` is always binary. (Which is why `x <<= 1` multiplies by 2, not 10 or 16). Of course, you do sometimes need to write programs to serialize a number into a hex string, such as [How to convert a binary integer number to a hex string?](https://stackoverflow.com/q/53823756) – Peter Cordes Mar 29 '22 at 04:38

4 Answers4

4

In almost all cases, all data utilized by the computer are ultimately expressed in binary. The computer merely converts the binary data into a decimal or a hexadecimal representation in character digits. There is no space efficiency of hexadecimal over decimal.

Hexadecimal is preferred over decimal because it is a power of 2 and it utilizes all 10 decimal digits plus 6 letters. It effectively compresses a binary expression into a more readable form by treating each hexadecimal digit as a series of four binary digits. This cannot be done for decimal.

Compare this: Which is more readable?

  • FEFF in hexadecimal
  • 1111111011111111? in binary
1

Computer manipulate binary data, representation is in form of binary.It is easy to map binary to base 16(hex) than base 10(decimal), in hexadecimal conversion each group of four digits converted directly to hexadecimal.

The advantage of hexadecimal is ultimately for Mapping purpose.

1

Hexadecimal numbers are more readable and compact than binary strings and thus easier for human beings to work with. So for example when a programmer creates or maintains a configuration file with the colors for a website, or a constants file with numeric constants, working with hexadecimal numbers is easier than working with long binary strings.

For example, FF0000 (red) is more compact and readable than 11111111, 00000000, 00000000 which is red in binary.

Now decimal could also be used here instead of hexadecimal, but it is less space efficient than hexadecimal because a hexadecimal string can represent a much bigger range of numbers than a decimal string of the same length.

For example, FFFF, the largest hexadecimal number of 4 digits is equal to the decimal number 65535 which is far bigger than 9999, the largest decimal number of 4 digits.

Also, of course Hexadecimal digits (0-9, A-F) use all 16 possible combinations of a 4 bit binary string while Binary digits (0-9) use 10 of the 16 combinations.

SR Bhaskar
  • 898
  • 7
  • 17
0

An ASCII string of base-16 digits can represent the same number in fewer characters than an ASCII string of base-2 digits. But the original number itself is normally stored in binary, if we're talking about an integer in a memory location or register, or a variable value in a high-level language. (It is possible to write programs that work with BCD, sequences of decimal digits, or other non-binary representations of numbers.)

Hex and decimal are just serialization formats for numbers as strings of digits; a C int is always binary. (Which is why x <<= 1 multiplies by 2, not 10 or 16; this is a fact defined by the C language standard, which makes sense given that they want it to be implementable on real world binary computers which also work that way. Similarly x & 1 == 0 is the same as x % 2 == 0.)

Of course, you do sometimes need to write programs to serialize a number into a hex string, such as How to convert a binary integer number to a hex string? (in x86 assembly).

Related: MIPS Assembly converting integer to binary and reading the number of 1's? goes into more detail about integers in CPU registers already being in binary.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847