K
kernelxu
Hi,folks.
I got some suggestion about bitwise shift from <The C Book, second
edition>(written by Mike Banahan, Declan Brady and Mark Doran,
originally published by Addison Wesley in 1991. This version is made
freely available at http://publications.gbdirect.co.uk/c_book/)
....snip...
The position is clearer if an unsigned operand is right shifted,
because there is no choice: it must be a logical shift. For that
reason, whenever right shift is being used, you would expect to find
that the thing being shifted had been declared to be unsigned, or cast
to unsigned for the shift, as in the example:
int i,j;
i = (unsigned)j >> 4;
....snip...
However, when I run the following program with some little changes,
the result confused me.
statement 1:
the printed results of variables m and n are different.
replacing statement 1 with statement 2:
the same as above.
replacing statement 1 with statement 3:
the printed results of variables m and n are the same, which is what I
expected.
So, I'am sure the statements 1 and 2 are incorrect.
The variables i and n both have the type of unsigned char,
is the type of "n<<i"unsigned char?If so, is the type cast necessary?
or, if not, is that the reason of integer promotion?
What about statement 2 comparing to statemnt 1?
IMHO, they are the same.
Any comments are appreciated in advance.
/**************bitwise_shift.c**************/
#include <stdio.h>
int main(void)
{
unsigned char c = 0x79;
unsigned char m;
unsigned char n;
unsigned char i = 2;
printf("sizeof(unsigned char) = %u\n", sizeof(unsigned char));
m = c;
m <<= i;
m >>= 7U;
printf("m = %c\n", m);
n = c;
n = (n << i) >> 7U; /*statement 1*/
/*n = (n << 2U) >> 7U;*/ /*statement 2*/
/* n = (unsigned char)(n << i) >> 7;*/ /*statement 3*/
printf("n = %c\n", n);
return 0;
}
/***************end of bitwise_shift***************/
I got some suggestion about bitwise shift from <The C Book, second
edition>(written by Mike Banahan, Declan Brady and Mark Doran,
originally published by Addison Wesley in 1991. This version is made
freely available at http://publications.gbdirect.co.uk/c_book/)
....snip...
The position is clearer if an unsigned operand is right shifted,
because there is no choice: it must be a logical shift. For that
reason, whenever right shift is being used, you would expect to find
that the thing being shifted had been declared to be unsigned, or cast
to unsigned for the shift, as in the example:
int i,j;
i = (unsigned)j >> 4;
....snip...
However, when I run the following program with some little changes,
the result confused me.
statement 1:
the printed results of variables m and n are different.
replacing statement 1 with statement 2:
the same as above.
replacing statement 1 with statement 3:
the printed results of variables m and n are the same, which is what I
expected.
So, I'am sure the statements 1 and 2 are incorrect.
The variables i and n both have the type of unsigned char,
is the type of "n<<i"unsigned char?If so, is the type cast necessary?
or, if not, is that the reason of integer promotion?
What about statement 2 comparing to statemnt 1?
IMHO, they are the same.
Any comments are appreciated in advance.
/**************bitwise_shift.c**************/
#include <stdio.h>
int main(void)
{
unsigned char c = 0x79;
unsigned char m;
unsigned char n;
unsigned char i = 2;
printf("sizeof(unsigned char) = %u\n", sizeof(unsigned char));
m = c;
m <<= i;
m >>= 7U;
printf("m = %c\n", m);
n = c;
n = (n << i) >> 7U; /*statement 1*/
/*n = (n << 2U) >> 7U;*/ /*statement 2*/
/* n = (unsigned char)(n << i) >> 7;*/ /*statement 3*/
printf("n = %c\n", n);
return 0;
}
/***************end of bitwise_shift***************/