I am very interested in working with binary , hexadecimal and octal systems in mysql database. First of all please give me an advice why we need them during storing information , because of ton of information or why ?
Also which type of values must be stored in marked systems ?
In addition here are bit operator like “<<“
here is example => SELECT 50<<2 AS example;
this gives us result 200 , anyone can explain how it is calculating it ?
Thanks for answering :))
Computers store data in binary. Sometimes it’s useful for us to think in terms of the actual bits that are stored, in which case our familiar decimal system can be a little awkward (as conversions are not straightforward); we could write the bits out in full, but that’s often too cumbersome since even quite small numbers take up a lot of space to write (e.g. decimal
24521is binary101111111001001).Instead, we tend to use bases which are some power of 2, since they’re more compact than binary whilst having the property that each ‘digit’ represents an exact number of bits in the binary representation. For example, a hexadecimal (base-16) digit represents four bits (a “nibble”) with the digits
0through toF(decimal15/ binary1111); an octal (base-8) digit represents three bits with the digits0through to7(binary111).Our earlier example of decimal
24521would be5FC9in hex or57711in octal: starting from the right you can see that each digit respectively represents 4 and 3 bits in the above binary representation. Therefore it is (relatively) easy for us humans to visualise the binary representation whilst looking at these compact representations in other bases.I’m not sure what you mean by this. As indicated above, the same values can be represented in all of these systems. In MySQL, we can indicate a binary literal by prepending it with
0band a hexadecimal literal by prepending it with0x. MySQL does not support octal literals.The
<<operator performs a bitwise left-shift. That is, it shifts the bits of the left-hand operand left by the number of places given by the right-hand operand.For each position the bits of an integer are shifted left, the value represented by those bits increases two-fold. It’s similar to the effect of shifting digits left in our decimal system, whereby values increase ten-fold (for example, 50 shifted one place to the left gives 500, which is a ten-fold increase; in binary
110(decimal 6) shifted one place left gives1100(decimal 12), which is a two-fold increase).In your case, shifting the bits of the number 50 (i.e.
110010) two places to the left yields 2 two-fold increases (i.e. a four-fold increase overall):11001000is decimal 200.