Before we start coding, let’s practice talking in binary. Remember, the ASCII chart where every alphabets, numbers are given an equivalent code, for example, Char A is 65 in decimal, Char single quote (‘) is 39 in decimal.
How do we represent a number in decimal? We use the decimal place value (with base 10) and do this like below.
If I want to do the same with binary (with base as 2), I will again use the place value, but of binary.
So, to represent 39 in decimal, which cards would you use?
So, if you mark position of the cards used it will be 100111 and that is 39 in binary. Alright!
In code, there are several techniques to calculate which bit places to set with ‘1’ or ‘0’. The most common ones are:
1. Continuous Division by 2 (classic math)
Continuous division is about sharing and dividing 39 into smaller groups of 2, (the reminders at each step will give you the binary digits) until you can’t divide anymore.
And here is your code which you can call ft_atob_division.c
(ascii to binary).
#include <stdio.h>
int main()
{
int asciiCode = 39;
// Convert ASCII code to binary representation
int binaryDigits[8]; // Assuming 8-bit ASCII representation
int i;
// Loop to set binary digits
for (i = 7; i >= 0; i--)
{
binaryDigits[i] = asciiCode % 2;
asciiCode /= 2;
}
// Print the binary representation
printf("Binary representation of single quote: ");
for (i = 0; i < 8; i++)
{
printf("%d", binaryDigits[i]);
}
printf("\n");
return 0;
}
Now, I will leave it to you to figure out how you will convert from binary to ascii to write your own ft_btoa_exponent.c
(by interpreting the binary digits using the powers of 2).
2. Bit shifting (in style, with bit wise operators)
This is another way of converting decimal into binary. Before trying to understand what this stylish technique means, what if I told you what you see in your code as ‘decimal’, is not real?
By default, in your code when you say
int asciicode = 39;
it is not 39, but instead it is already in binary form 😱
I am sure, you now realize the truth.
Do you see the little spoon below? Huh, 😂 sorry.
Do you see the little calculator below? That will help you to see what happens under the hood (code).
int asciicode = 39;
Let’s only consider the last 8 digits of the binary, which is (0010 0111)
I will peek into the 8 digits, starting from the left most (here is where bit shifting comes into play, since I am going from left to right, I am using the “bit shift right (>>)” symbol.
int number;
for (i = 7; i >= 0; i--)
{
number = (asciiCode >> i);
}
What do you think the ‘number’ will be each time? You can notice below, that the binary of 39 (0010 0111), is
when i = 7: shifted right seven positions, hence the most significant bit (MSB) — 0, is the result of the operation
= _______0 decimal equivalent (0);the rest is ignored 010 0111
Similarly,
when i = 6: shifted right six positions, hence the result of the operation = ______00 decimal equivalent (0);the rest is ignored 10 0111
when i = 5: shifted right five positions, hence the result of the operation
= _____001 decimal equivalent (1); the rest is ignored 0 0111
when i = 4: shifted right four positions, hence the result of the operation
= ____0010 decimal equivalent (2); the rest is ignored 0111
when i = 3: shifted right three positions, hence the result of the operation
= ___0010 0 decimal equivalent (4); the rest is ignored 111
when i = 2: shifted right two positions, hence the result of the operation
= __0010 01 decimal equivalent (9); the rest is ignored 11
when i = 1: shifted right one position, hence the result of the operation
= _0010 011 decimal equivalent (19); the rest is ignored 1
when i = 0: shifted zero position, hence the result of the operation
= 0010 0111 decimal equivalent (39);
In the previous paragraph, when I said “I will peek into”, I really meant to see the “nth bit” if it is ‘0’ or ‘1’ that’s all. I didn’t want to find what number it represents. So, I will now use a filter (bit mask) that will only reveal the number in the ‘nth’ position. the ‘nth bit’ when AND (added) with ‘0x01’ will tell me whether it is a ‘0’ or ‘1’.
The code below is another version of ascii to binary ft_atob_bitshift.c
These will be nice additions to your libft, for future use.
#include <stdio.h>
int main()
{
// ASCII code for single quote
int asciiCode = 39;
// Convert ASCII code to binary representation using bit shifting
int binaryDigits[8]; // Assuming 8-bit ASCII representation
int i;
// Loop to set binary digits using bit shifting
for (i = 7; i >= 0; i--) {
binaryDigits[i] = (asciiCode >> i) & 1;
}
// Print the binary representation
printf("Binary representation of single quote: ");
for (i = 7; i >= 0; i--) {
printf("%d", binaryDigits[i]);
}
printf("\n");
return 0; */
}
To summarize the whole thing, Bit shifting is like peeking at binary code, moving to the nth place (>> i) of the bit of the 8-bit code, and checking if there is a ‘1’ or ‘0’ (&1). That's how we extract individual digits in a super simple way ;)
Again, I will leave it to you to figure out the binary to ascii conversion and write your own ft_btoa_bitshift.c
. You will need it on your server, to convert the binary into ascii and print it in its end.
Enough of binary talking. Now it is time to let the client send signals!