Binary, hexadecimal and octal values ​​in mysql

I am very interested in working with binary, hexadecimal and octal systems in mysql database. First of all, please give me advice on why we need them during the storage of information, because of the ton of information or why?

And what type of values ​​should be stored in marked systems?

Also here is a bit operator such as "<"

here is an example => SELECT 50<<2 AS example;

this gives us a result of 200, can anyone explain how he calculates it?

Thanks for the answer:))

+4
source share
2 answers

First of all, give me advice on why we need them when storing information.

Computers store data in binary format. Sometimes it’s useful for us to think about the real bits that are stored, in which case our familiar decimal system may be a bit uncomfortable (since the conversions are not simple); we could write the bits in full, but this is often too cumbersome, since even fairly small numbers take up a lot of recording space (for example, decimal 24521 is binary 101111111001001 ).

Instead, we tend to use bases that are some power of 2 because they are more compact than binary ones but have the property that each “digit” represents the exact number of bits in the binary representation. For example, a hexadecimal (base-16) digit represents four bits ("nibble") with digits from 0 to F (decimal 15 / binary 1111 ); the octal (base-8) digit represents three bits with the digits 0 to 7 (binary 111 ).

Our previous decimal example 24521 would be 5FC9 in hexadecimal or 57711 in octal: starting from the right side you can see that each digit represents 4 and 3 bits respectively in the binary representation above. Therefore, it’s (relatively) easy for us to visualize the binary representation of people by looking at these compact representations in other databases.

And what type of values ​​should be stored in marked systems?

I'm not sure what you mean by that. As indicated above, the same values ​​can be represented in all of these systems. In MySQL, we can specify a binary literal by adding it with 0b and a hexadecimal literal by adding it with 0x . MySQL does not support octal literals.

can anyone explain how he calculates it?

The << operator performs a bitwise left shift. That is, it shifts the bit of the left operand to the left of the number of places specified by the right operand.

For each position, the bits of the integer are shifted to the left, the value represented by these bits is doubled. This is similar to the effect of shifting the digits left in our decimal system, and the values ​​increase tenfold (for example, 50 shifted one place to the left gives 500, which is a tenfold increase, in binary format 110 (decimal value 6)), shifted by one place on the left gives 1100 (decimal 12), which is a double increase).

In your case, the shift of the bits of the number 50 (i.e. 110010 ) in two places on the left gives 2 times twice (i.e. the total increase is four times): 11001000 - decimal 200.

+4
source

Your first two questions are too vague to answer, but the third of them is specific enough, I will answer.

<<2 shifts bits to the left by 2 places, as described here . This is equivalent to multiplying 50 by 2 ^ 2:

 mysql> SELECT 50<<2 AS example; +---------+ | example | +---------+ | 200 | +---------+ 1 row in set (0.00 sec) mysql> mysql> SELECT 50 * POW(2,2) AS example; +---------+ | example | +---------+ | 200 | +---------+ 1 row in set (0.00 sec) 
+2
source

Source: https://habr.com/ru/post/1412852/


All Articles