I am trying to solve a C programming problem, which is date encoding in 2 bytes using bit shifting operations and decoding back to date, month and year. While I could also use bitwise &
and |
operation, I want to use just bit shifting.
The issue is: even though everything seems to be right the output of date and month has no effect of dual bit shifting operation. The year is okay because there is only one operation. Meaning that the dual operation of bit shift is somehow not behaving as required.
I have used unsigned char
so last sign bit is definitely not the issue. The Showbits
function is just to show the bits. I am using gcc g++ compiler with VSCode in Windows 10.
// Date encoding in 2-byte number
#include <stdio.h>
#include <conio.h>
typedef unsigned int uint;
Showbits(int n)
{
int i, k, mask;
for (i = 15; i >= 0; i--)
{
mask = 1 << i;
k = n & mask;
k == 0 ? printf("0") : printf("1");
}
}
int main()
{
uint encd_date, date, month, year;
year = 2022;
month = 9;
date = 15;
encd_date = 512 * (year - 1980) + 32 * month + date;
printf("\nEncoded date:%u\n", encd_date);
printf("\nencd_date: ");
Showbits(encd_date);
year = (1980 + (encd_date >> 9));
month = (encd_date << 7);
month = (month >> 12);
date = (encd_date << 11);
date = (date >> 11);
printf("\ndate: ");
Showbits(date);
printf("\nmonth: ");
Showbits(month);
printf("\nyear: ");
Showbits(year);
printf("\nDecoded date %u month %u year %u", date, month, year);
return 0;
}