What are Decimal, Hexadecimal and Octal notation numbers?

12

What are Decimal (base 10), Hexadecimal (base 16), and Octal (base 8) notation numbers? In the book the code looks like this:

<?php    

    // numero decimal
    $a = 1234;

    // numero octal (equivalente a 83 em decimal)
    $a = 0123;

    // numero hexadecimal (equivalente a 26 em decimal)
    $a = 0x1A;

?>
    
asked by anonymous 22.01.2015 / 13:53

3 answers

14

This is known as positional notation . It's the way we represent numbers.

Decimal

The decimal representation everyone knows, goes from 0 to 9 and how far I know it's because of the number of fingers the human has. It's the way humans grow accustomed. Each position on the left adds the total units of the representational system, ie in the decimal each extra house on the left is worth the number represented 10 times.

Hexadecimal

The hexadecimal representation goes from 0 to 15. This is closer to how the functional computer is. Since everything in the computer is binary, everything goes up with base 2, numbers that programmers consider "round" are 2, 4, 8, 16, 32, 64, 128, 256 and more that this would need more than one byte to represent. It is easier and more linear to represent these numbers based on 16, go from 0 to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). Then 1A is the same as 26 in decimal since 1 in this case is it times 16 (the total number that can be represented in hexa plus 10 which is the value of A.

Note that to differentiate from the hexadecimal literal in most languages, you should start with 0x .

Colors are an example to anyone developing to web using hexadecimal. The% color of% is absolute white. The first FFFFFF says that it is the maximum red, or 255. Then the FF indicates the maximum green and the last FF indicates the maximum blue. When all the colors in their fullness are combined, we have the white. To represent the same thing in decimal form would have to be FF . The comma is required because it is common that the decimal can have a variable number of digits and in hexadecimal it is common to use a fixed number of digits in case 2, since with 2 digits we can represent all possible numbers in a byte .

Octal

Octal was used on computers with a specific architecture, today it has little relevance. It can still be used to represent situations where the base must be 8 (from 0 to 7), but it is rare to have effective use. Therefore to represent decimal 8 in octal would be 10. Note that it follows the hexadecimal model using a binary-based ratio. Many languages no longer support this representation. Normally a leading zero is used to indicate that the representation is octal. This can generate bugs in some cases and the programmer does not take care.

Binary

In addition, some languages use binary representation that is the most basic for computers. This is simple since it has only two possible digits, 0 and 1. Since the base is 2, each extra digit on the left is raised to 2. With 8 digits we have a byte , the same 256 different values when we use 2 digits in hexadecimal. This is a useful representation in limited cases but can be useful when the data is composed of Boolean information in some way, when it needs to show on and off states.

Conversion

In general, to convert from a smaller base to a larger base, the multiplication of each right-to-left digit of the number you want to convert by the number resulting from the base's power by a sequence of numbers starting at 0 (remembering that every number raised to zero results in 1).

To do the opposite operation you will make successive divisions using the base as a divisor and taking the rest of each division. The first part goes to the right and the rest goes to the left.

This is a simplification of conversion. You can not use it in any situation.

Closer to computer pattern

For computers 10 is a strange number, it is a representation used to make the life of a strange animal easier. But at the same time it can create some difficulties.

Almost all languages, from assembly to JavaScript, have at least the decimal and hexadecimal and they should be used when they are most convenient in each case. In PHP it is rarer to use hexadecimal in most cases.

Note that all these numbers represent the same number differently. For computers the number is represented in the same way, ultimately through electrons circling. The representation in the language code is something that only matters for humans to see better what they are doing, it is more an abstraction.

    
22.01.2015 / 14:29
6

Numbers always represent a quantity, but there are several ways to represent numbers. The most common is the decimal system, which uses 10 digits (from 0 to 9 ). And what do we do in the decimal system to express numbers that can not be represented with a single digit? We added a second digit. See:

              10       
 uma dezena --^^-- zero unidades
       10      +    0     =>    10
 
              13       
 uma dezena --^^-- três unidades
       10      +    3     =>    13

The decimal system uses base 10 , that is, with 10 units we need a leftmost house (the tens house) to represent the number. In the same way, every ten decades we need another house, the hundreds:

               115       
 uma centena --^^^-- cinco unidades
                |--- uma dezena
       100   + 10   +   5     =>    115

But base 10 is not the only way to represent numbers. You can use any base. The binary system uses base 2, which has only two digits, 0 and 1 . So in binary:

0   =>   0
1   =>   1
10  =>   2  (1 * 2 + 0)
11  =>   3  (1 * 2 + 1)
100 =>   4  (1 * 4 + 0 + 0)
...

The octal system uses base 8, which only has the digits of 0 to 7 . So in octal:

...
6   =>   6
7   =>   7
10  =>   8
11  =>   9  (1 * 8 + 1)
12  =>   10 (1 * 8 + 2)
...
20  =>   16 (2 * 8 + 0)
...
100 =>   64 (1 * 64 + 0 + 0)

The hexadecimal system uses sixteen digits: the digits 0 to 9 plus the letters A to F . So:

 9  =>   9
 A  =>   10
 B  =>   11
 C  =>   12
 D  =>   13
 E  =>   14
 F  =>   15
10  =>   16  (1 * 16 + 0)
    
22.01.2015 / 14:46
0

John ... this has more to do with computer architecture than with PHP itself, as the name itself already says, are base numbers X, converted to another base.

To better elucidate the answer:

They are very useful in low-level languages ... such as C , Cobol , Fortran , Assembly for example.

    
22.01.2015 / 14:02