What Are Bits and Why Are They Important
A bit or “binary digit”, is the most basic representation of data for computers. There are only two possible values used to represent a binary digit, 1 and 0. A combination of 1’s and 0’s are used to represent numbers with bits, such as the number 26. We can represent 26 by dividing the number by 2 and then using the remainder to determine whether to use a 0 or 1. Then we display our number in reverse order from what we calculated to provide the binary representation of the number. Let’s break it down in the example below.
- 26/2 = 13, remainder: 0
- 13/2 = 6, remainder: 1
- 6/2 = 3, remainder: 0
- 3/2 = 1, remainder: 1
- 1/2 = 0, remainder: 1
Once we finish dividing 26 by two until we reach 0, we are given a value of 01011. However, the final step is to reverse the order to get the final answer. Thus, the number 26 is represented in binary form as 11010.
const twentySix = 0b11010;
Number.toString(base you’d like to transform Number);
We start by typing the number followed by the toString() function and then including the base number in the parameter. Let’s look at the return value for the number 26 converted into binary in our console.
We can see that the binary representation of the number 26 is “11010”, which is also the answer we got above when we converted the number by hand.
Now, let’s see how we can transform numbers using the parseInt() method! This is more often used to convert numbers of different bases to Number primitives. The syntax for the parseInt() method is:
parseInt(Number, base of Number currently);
Since we transformed a decimal into a binary number using the toString() method, let’s use the parseInt() method to find the decimal value of a binary number. Below is an example of us converting “11010” to the decimal 26.
We can see from the example, the number 11010 is established as binary in the second parameter (base 2), which gives binaryNumber a value of 26. converted to the decimal number 26 when it is given the base of 2 to translate the binary number.
Before we move on into bitwise operations, I want to make it clear that when using toString() and parseInt(), they are not interchangeable. We use toString() when we are trying to convert a base 10 number (decimal) to a Number of a different base. We use the parseInt() method when we are attempting to change numbers of different bases back into a decimal or Number with base 10. As long as you understand that, transforming Numbers is not too difficult!
Bitwise Operations and How To Manipulate Bits
Now that we know what bits are and how to transform any integer to a binary number, let’s get into some different operations that will help us manipulate bits and have them work for us. I will be using the operations in examples and I encourage you to follow along!
Slight But Important Detour
Before we get started with bitwise operations, there is an important note about binary numbers that I wanted to provide to you. I want to discuss how we write a negative number in binary. When establishing whether a binary number is positive or negative, we use what’s called a signed number. Signed binary numbers use the leftmost digit to determine whether the the number is positive or negative. We can also refer to the leftmost digit as the MSB or “most significant bit”. If the binary number is negative the MSB will be 1, and if it is positive the MSB will be 0. Now that you know how to represent negative and positive binary numbers let’s go through some different bitwise operations that will help you manipulate some bits!
& (AND) Operator
The bitwise AND operator (&), when applied to 2 binary numbers, will return 1 whenever the corresponding bits of both are 1. Let’s look at our example below to get a better understanding of what’s happening!
Let’s start where I declare the constant twelve and twentySeven as binary numbers using the “0b” prefix before the binary representations of the numbers. Then I declare the value of and as twelve & twentySeven, using the & bitwise operator, and received a value of 8 (decimal). How did we get 8? Well, let’s take both binary numbers and break down what is actually happening.
twelve = 1100
twentySeven = 11011
If you see from the example above, the & operator takes both binary numbers and compares corresponding digits to return the binary number 1000. If the corresponding digits are both 1, the operator will return a 1, otherwise it will return a 0. The binary number 1000 represents the value of 8 in decimal terms and thus the value of and is 8. We can test this out by using the toString() method, which we learned how to use above, to return the binary number representation of the variable and.
| (OR) Operator
The OR operator (|) is a bitwise operator that will return 1 whenever one of the corresponding bits of the operand is 1. Let’s use the same example as above, but use the OR operator to see how it works.
So from the console above, when we use the OR operator with twelve and twentySeven, we get the decimal number of 31. Let’s got to the drawing board to see how we got that!
twelve = 1100
twentySeven = 11011
We go back to our board and now it will return 1 whenever at least one of the digits contains a 1. You can see from the return result above that we get the binary number of 11111. If we convert 11111 into a decimal we get 31, which is why the variable or in the console above returns a value of 31. We can check that the math in our drawing board was correct by calling the toString() method on or and returning the binary representation of 31, which we can see is 11111.
The XOR operator, also known as the exclusive OR, is an operator that will return 1 whenever the two corresponding bits are different from each other. This means that any combination of zeros and ones will result in 1. Let’s take our example above and use the XOR operator on twelve and twentySeven.
We can see from the example above that when we use the XOR operator above on twelve and twentySeven, we get a value of 23 or in binary 10111. Let’s break the return value down like we have for the last two operators on the drawing board!
twelve = 1100
twentySeven = 11011
We can see from the drawing board that when comparing the corresponding digits, whenever the two numbers are different from each other a 1 is returned, and when they are the same a 0 is returned. This is how the value of the xor constant is equal to 23 because its binary representation is 10111. We can check that we completed the proof above correctly by calling the toString() method on xor.
The last operator I want to go over in this blog is the NOT operator. It is a pretty simple operator because all it does is invert the operand. This means when the digit is 1 it will return a 0 and vice versa. Let’s look at the NOT operator in action with our example.
When you look at our console, we establish that the not variable is equal to the inverse of twentySeven, which we see is -28. The NOT operator transforms positive numbers into negative numbers and vice versa because as I mentioned earlier, we use the leftmost bit of a binary number to indicate its signal. But, this does not mean that we simply add a 1 or zero before the number, the operator is applied to each bit of a number individually. So, of the binary number 11011 (27, decimal) the return value would be 00100. The return value of a NOT operator or the inverse of the Number is also known as its complement.