I musn't be thinking about something right (or maybe left)! If I have the number '1', which in binary is this...
1000 0000
...and I do a right shift on it to get this...
0100 0000
then that should give me '2', not??? Then why doesn't this work...
#include <stdio.h>
int main(void)
{
char x=1;
x=x>>1;
printf("x=%d\n",x);
getchar();
return 0;
}
//x=0
Doesn't x=x>>1 mean 'Shift 1 to the right 1 bit? Isn't that 2? Here is my program to shift 1 across all 8 bits. Notice I'm using left shift - not right shift. That doesn't make sense to me. The output you see below the program is what I want (what I'm wanting to create is an and mask), but I'm having to use the bit shift operator backwards from what I'm thinking it should be. Can someone please set my head right (or left) on this??????!!!!!!
#include <stdio.h>
int main(void)
{
unsigned char x=1;
printf("i\t1<<i\n");
printf("==============\n");
for(unsigned i=0; i<8; i++)
{
x=1<<i;
printf("%u\t%d\n",i,x);
}
getchar();
return 0;
}
/*
i 1<<i
==============
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
*/