I’ve always been pretty intrigued by seeing a bunch of 0’s and 1’s on computer screens, so I thought why not do some research on it. The decimal base 10 system or denary is already engrained in my brain, so learning about base 2(10 in the binary system) should be a breeze right?… I hope so.
The binary number system converts numbers by repeatedly dividing a decimal by 2 and it keeps track of the remainder. So if we wanted to convert decimal 7 to binary, you would do 7 / 2 get the remainder, then 3 / 2 get the remainder, and finally 1 / 2 and get the reminder and place them in reverse order.
For starters, a bit which is just short for binary digit is represented by 0 and or 1. One byte is composed of 8 bits and is the smallest addressable unit of memory for computing. A bit is also known as a single transistor. A transistor is a minute switch that represents 0 when off and 1 when it receives power and turns on. Our phones and computers have literally millions and billions of these transistors. I know what your thinking… THAT’S A BUNCH OF DARN TRANSISTORS! Well with all of the graphics we have and what these machines have to run, we need it. Our computers now can process millions of bits a second. These 0’s and 1’s represent numbers, letters, colors etc. that a computer needs in order for us to tell it what to do. Side note, hard drives story binary digits on discs rather than transistors.
Our computers encode and store everything in binary digits from letters(ex.ASCII), our audio files(ex.mp3), video files(ex.MPEG4), to pictures(ex.JPEG) which have bit depth. Upgrading from a byte to a 16bit line will give drastic more options.
A nibble is half of a byte or 4 bits(pretty funny right?). 8 bits = 1 byte, 1024 bytes = 1 kilobyte(KB), 1024 kilobytes = 1 megabyte(MB), 1024 megabytes = 1 gigabyte(GB), 1024 gigabytes = 1 terabyte(TB), 1024 terabytes = 1 petabyte(PB). If you think about a 64gb iphone, in bytes that number is actually 68719476736.
A hard-drive is typically measured in GB or TB. RAM is usually in MB or GB.
In many situations you might need to alter your certain bits in a byte and for that you would need to use bitwise operations. There are 6(110 in binary) main bitwise operators, which are bitwise AND, bitwise OR, bitwise exclusive XOR, bitwise Complement or NOT, bitwise Right Shift, and bitwise Left Shift. Bit masking is used to turn individual bits on or off. This only applies to integer values.
Bitwise AND & needs two operands. The bits are compared to each other and when both bits are 1, the result is 1, otherwise the result is 0.
00001010 = 10 &
00000111 = 7
00000010 = 2
Bitwise OR | needs two operands. The bits are compared to each other and when any of the bits is 1, the result is 1.
00001010 = 10 |
00000111 = 7
00001111 = 15
Bitwise XOR ^ needs two operands. The bits are compared to each other and if one of the bits is 0 and the other is 1 , the result is 1, else the result is 0. It returns 1 when the two bits are different.
00001010 = 10 ^
00000111 = 7
00001101 = 13
Bitwise NOT ~ flips the bits of the operand. This results in the negative of a number with -1, for the most significant bit to show the sign of the number where 1 is negative and 0 is positive.
~00001010 = 11110101
Bitwise Right Shift >> needs two operands. In the first operand, the bits are shifted to the right by the number of positions identified in the second operand. 0’s are shifted to higher order bits while low order bits are shifted out completely. Shifting right is like dividing by 2.
(5)00000101>>2 = 00000001(1)
Bitwise Left Shift << needs two operands. In the first operand, the bits are shifted to the left by the number of positions identified in the second operand. 0’s are shifted to low order bits while high order bits are shifted out completely. Shifting left is like multiplying by 2 and doubles in regards to the second operand.
(5)00000101<<2 = 00010100(20)
The picture above helped me gain a better understanding of what is going on.
Now lets see how we can use Ruby to perform these operations.
Above whats going on is, if you type in ‘0b’ then they eight digit byte to a base (2) and put it to a string (to_s) it will convert it to decimal for you or perform the bitwise operation that you are telling it. You can also apply it to a decimal number such as the not operation above to 17 and see what it returns, even though negative binary is another little thing of it’s own that we won’t really get into.
Above, we are setting decimal 58 to a string to see it’s binary. Then we are asking what is the decimal number for the provided binary. Then I am asking what is the bit for a particular position in it’s byte. Then if you ask the bit length for any decimal number it will return the bit length of it. The max number for a byte is 255, that is why after that we get 9 when we ask for the bit length of 257.
In the image above .ord returns the integer ordinal for a specific character. Welcome to ASCII. Then you can find what character is associated with that number you provide if you type in .chr. When you provide a string, then set it to .bytes you will get back the bytes for each individual letter. This is pretty much the same as .codepoints. However above these match up but, lets say you have a different language like Arabic, the .codepoints would not be the same. Lastly .chars just splits the string up of each individual character into an array.
Don’t get confused about the shovel method in ruby with the bitwise shift lol.
One last thing is negative numbers in binary digits. I find it a little weird but you can represent negative numbers in a few ways. You can use the outside number on the left to make it negative by making it a 1 or positive by 0. || you could use something called two’s complement.
I enjoyed learning about bits and bytes and how they correlate with computer components. Before I dug into binary digits, I read a book a few months ago called Bitwise, a life in code by David Auerbach which was I guess my first intro to bits. Even though the book is more a a memoir about his life as a software engineer and being married to a software engineer than it is about a bunch of bits, it was a very nice read.
There is more to 0’s and 1’s, but this is where I’m going to end at. I hope you enjoyed &| learned anything from this.
Very helpful resources and videos:
- Sololearn app