So I am doing some bit-shifting and i have come to the following problem, that i would be more than grateful to get an answer to:
As an argument I'm allowed to pass the size of 1 byte.
The first 4 bits represent a numerator. The last 4 bits represent the denominator.
The following code works and gives the correct output:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char** argv)
{
for(int i = 1; i < argc; i++)
{
unsigned char numerator = atoi(argv[i]);
numerator = (numerator >> 4);
numerator = (numerator << 4);
unsigned char denominator = atoi(argv[i]);
denominator = (denominator << 4);
denominator = (denominator >> 4);
printf("%d/%d\n", numerator, denominator);
}
return 0;
}
But if i substitute the bit shifting part like this, the denominator gives the same output as the numerator:
unsigned char numerator = atoi(argv[i]);
numerator = (numerator >> 4) << 4;
unsigned char denominator = atoi(argv[i]);
denominator = (denominator << 4) >> 4;
sample input would be:
./test 1
./test 16
output given:
0/1
16/16
expected output:
0/1
16/0
Thanks in advance for any sort of help.