What are decimal, hexadecimal, and octal notation numbers?

Asked

Viewed 4,364 times

13

What are decimal (base 10), hexadecimal (base 16) and octal (base 8) notation numbers? In the book the code is 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;

?>

3 answers

16


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

Decimal

To decimal representation Everybody knows it, it goes from 0 to 9 and as far as I know it’s like this because of the number of fingers the human has. It’s the way humans grow used to it. Each position on the left adds the total units of the representational system, ie in the decimal each extra box on the left is worth the number represented times 10.

Hexadecimal

To hexadecimal representation 0 to 15. This is closer to how the computer works. Since everything in the computer is binary, everything is scaling based on 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 becomes easier and more linear to represent these numbers based on 16, to go from 0 to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). So 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 the hexadecimal literal from the decimal, in most languages, it must start with 0x.

Colors are an example for those who develop for web hexadecimal usage. The color FFFFFF is the absolute white. The first FF says it’s the maximum red, or 255. Then the FF next indicates the maximum green and the last FF indicates the maximum blue. When all colors in all their fullness are combined, we have the white. To represent the same thing decimally would have to be 255, 255, 255. The comma is necessary because it is common for the decimal to 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 the possible numbers in a byte.

Octal

Octal was used in computers with a specific architecture, today has little relevance. It can still be used to represent situations where the base should be 8 (from 0 to 7), but it is rare to have effective use. Therefore to represent the decimal 8 in octal would be 10. Note that it follows the hexadecimal model using a ratio based on binary. Some languages no longer support this representation. Usually an initial 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 which 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 interesting when the data is composed of Boolean information in some way, when it needs to show on and off states.

Conversion

In a general way to convert from a smaller base to a larger one, you multiply each digit from right to left of the number you want to convert by the resulting number of the base power by a sequence of numbers starting at 0 (recalling that any number to 0 results in 1).

To do the opposite operation go making successive divisions using the base as divisor and catching the rest of each division. The first rest goes to the right and the rest are placed to your left.

Divisões sucessivas para achar o binário

This is a simplification of conversion. Can’t use in any situation.

Closest to the computer pattern

For computers, 10 is a strange number, it is a representation used to facilitate the life of a strange bug, at the same time it can create some difficulties.

Almost all languages, from Assembly Javascript, have at least decimal and hexadecimal and they should be used when they are most convenient in each case. In PHP it is more rare 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 circulating electrons. The representation in the language code is something that matters only for humans to see better what they’re doing, it’s more an abstraction.

Textual representation

What you see of number on the screen is a textual representation of the number and not the number itself. Just like on paper, you have a textual representation, not a number. I know it can be hard to understand this because you’ve always gotten used to understanding that what you’re seeing there on paper is the number, but the number exists in nature, and there are diverse representations of it, you see a pair of stones can represent the number 2. Having electrons in a certain organization can represent numbers, what you see is just a text representing a number.

So when someone talks to convert a number from one notation to another it is usually talking about converting one textual representation to another.

In fact often it’s not even done, it’s just taking a number and generating a certain textual representation. One of the reasons this happens is that it is normal to always have in the language a way to print the text of numbers in a natural way, so that people do not even realize that there is a conversion there. It is common to print in other notations, and would not need to do any algorithm, but teachers and authors often put this as an exercise to do manually. They help amplify the myth that numbers are decimal because they don’t tell you to convert to decimal notation (that’s what I always say, people repeat what they’ve seen others do, they don’t understand what you’re doing).

  • 2

    I think it would look good an example of conversion between them and perhaps an aside with the binary system as well ;)

  • 1

    @Jorgeb. soon you come to me to talk about binary, would be off-topic :P You know I thought about putting and not put because I remembered you yesterday :) But I will improve yes.

  • Ahahah Yes but it is one thing to dwell on the subject, another is to complement the answer. ;)

8

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

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

Decimal system uses the base 10, that is, with 10 units we need a house to the left (the house of the tens) to represent the number. In the same way, every 10 tens we need yet another house, that of 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 basis. The binary system uses base 2, which has only two digits, 0 and 1. Therefore, in binary:

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

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

...
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 until F. Thus:

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

0

João.... this has more to do with computer architecture than with PHP proper, as the name itself says, are base numbers X, converted to another basis.

To better elucidate the answer:

Um exemplo de tabela de conversão de base de números

They’re very useful in low-level languages... like C, Cobol, Fortran, Assembly for example.

  • 6

    Google search is not the answer. Here is a Q&A site, we give answers that really answer the question, we don’t stay talking to users. Apart from that there are inaccuracies in the little information provided.

  • 1

    @bigow respect his opinion, but I did not send him to look, I advised a more thorough research, before asking questions that minimum search effort, could already solve... The question basically was, what are numbers at the base Hexa, Octal, Decimal, and to my mind, that question is already an answer of the same.... I do not see MUCH to talk about.... but OK, I’ll try to give you a better insight into the subject

  • @bigown agree, but also agree with Marcelo, because in fact the question seems to me somewhat incomplete, or too comprehensive, and whether or not, certain information is more easily obtained by researching than waiting for a ready answer, even more with no end (at least mentioned in the question).

  • 9

    If you have nothing to say, do not speak. This is not a forum. Answers should contain relevant knowledge. If the question is bad it is another problem. If we start telling users to search on the internet mischaracterizes completely the purpose of this website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.