8 min read
- What is Binary ?
- What is Byte ?
- What is Hexadecimal ?
As you may have heard, binaries nothing but 1s and 0s streaming through your hardware. In essence everything in our computers are binaries, but also in real life we somehow use binaries as well.
Logical example for binary would be a Light Bulb; a light bulb has only two states it's either open(1) or close(0). Now you may say, okay there are only 2 states in a light bulb, but how do only two digits make up all this stuff?
The good thing is we can combine the values like
1000 etc. With having more 1s in hand we can construct a meaningful things such as:
Colors, Strings, Numbers.
0b1111; // If you type this in your browser's console it will prompt: 15 parseInt(1111, 2); // This will also prompt: 15 Number(15).toString(2); // toString() turns your value into base-2 format which is binary
If you are wondering how
15 don't worry I got you. Key thing to remember everything is at base-2 format.
1 1 1 1 ↓ ↓ ↓ ↓ 2³ 2² 2¹ 2⁰ ↓ ↓ ↓ ↓ 8 + 4 + 2 + 1 = 15
Let's sum up Binaries
- It's in base-2 format.
- It's consist of bit.
- The prefix
Hexadecimals(also base 16) aren't so different than binaries. If we wanted to display decimal
255 in binary format we would have something like this:
0b11111111 8-bit long digits.
But, what if we wanted to display
65535. You see where this is going it's becoming more and more difficult to present them in binary(base-2) format. Instead of
displaying them as binaries people come up with hexadecimal to present them in more readable format.
|4095||0b1111 1111 1111||0xFFF|
|65535||0b1111 1111 1111 1111||0xFFFF|
To make these calculations at your browser:
0x1111; // If you type this in your browser's console it will prompt: 4369 parseInt(1111, 16); // This will also prompt: 4369 Number(4369).toString(16); // toString() turns your value into base-16 format which is hexadecimal
Let's see how does your browser make that calculation.
1 1 1 1 ↓ ↓ ↓ ↓ 16³ 16² 16¹ 16⁰ ↓ ↓ ↓ ↓ 4,096 + 256 + 16 + 1 = 4369
Now, if you are curious about
0xFF, in hexadecimal we are not limited by
0 we can also use other numbers up to
9, but what comes after
Now you have an adequate amount of knowledge to see why
Let's sum up Hexadecimals
- It's in base-16 format.
- It's consist of bytes.
- Each Hexadecimal digit represents four bits.
- The prefix
There are several operators:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Bitwise Left Shift
- Bitwise Right Shift
Bitwise AND (&)#
&) is quite simple it returns a
1 in each bit position for which the corresponding bits of both operands are
Practical example for
&) would be bitmasking. A bitmask is a pattern of isolating the positions of bits we're interested in.
Let's suppose we are only interested in first 8 bits of first binary.
0100010000110010 ^------^ Here is our 8bit 0000000011111111 ---------------- & 0000000000110010
Bitwise OR (|)#
|) similar to
And, but instead returns a 1 in each bit position for which the corresponding bits of either or both operands are 1s.
Bitwise XOR (^)#
^) returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1s.
Bitwise NOT (~)#
~) one of the easiest among others we simply invert 1s to 0s and 0s to 1s.
Bitwise Left Shift#
Left Shift operator
(<<) simply adds 0s to right of your binary by shifting others to left. This is generally used when we want to make room at the end of
Every shift left also multiplies your number as much as you shift left. If you shift
1 this will give us 28 since
0b1110 was 14.
Bitwise Right Shift#
Right Shift operator(
>>) deletes from right as much as your shift value.
That's all there is to it.
- Performing some operations at binary level will be faster than regular operations.
- The bitwise operators are mandatory if we have to deal with cases like bitmasking.
- Must learn the shifting operations if we are saving some space or clipping a part for future use.
Edit this page