一点一点地初始化错误 - 为什么不正确地设置最后两个变量的最高有效位?
wrong initialization of a short bit by bit - why set incorrectly the most significant bits of the last two variables?
我写这个程序是为了锻炼自己的位运算。在下面的代码行中,我尝试将值赋给三个 short
位。然后我通过 bit_print()
函数打印它们。
#include <stdio.h>
#include <limits.h>
void bit_print(short a)
{
int i;
int n = sizeof(short) * CHAR_BIT; /* in limits.h */
short mask = 1 << (n - 1); /* mask = 100...0 */
for (i = 1; i <= n; ++i) {
putchar(((a & mask) == 0) ? '0' : '1');
a <<= 1;
if (i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
}
int main(void){
short alice = 0, betty = 0, carol = 0;
int x;
char c;
printf("Put 16 bits for Alice: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
alice |= 1 << x;
else
alice |= 0 << x;
}
putchar('\n');
printf("Put 16 bits for Betty: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
betty |= 1 << x;
else
betty |= 0 << x;
}
putchar('\n');
printf("Put 16 bits for Carol: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
carol |= 1 << x;
else
carol |= 0 << x;
}
putchar('\n');
bit_print(alice);
putchar('\n');
bit_print(betty);
putchar('\n');
bit_print(carol);
putchar('\n');
return 0;
}
出于某种原因,如果我输入 1111111111111111 三次,执行程序我得到以下输出:
Put 16 bits for Alice: 1111111111111111
Put 16 bits for Betty: 1111111111111111
Put 16 bits for Carol: 1111111111111111
11111111 11111111
01111111 11111111
10111111 11111111
如您所见,最后两个变量 betty 和 carol 的最高有效位在不应该为零的地方有零。
为什么?
当使用 getchar()
时,你在输入结束时按下的换行符会被拾取,但你没有考虑它。
您需要阅读并丢弃每个循环末尾的换行符以说明这一点:
#include <stdio.h>
#include <limits.h>
void bit_print(short a)
{
int i;
int n = sizeof(short) * CHAR_BIT; /* in limits.h */
short mask = 1 << (n - 1); /* mask = 100...0 */
for (i = 1; i <= n; ++i) {
putchar(((a & mask) == 0) ? '0' : '1');
a <<= 1;
if (i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
}
int main(void){
short alice = 0, betty = 0, carol = 0;
int x;
char c;
printf("Put 16 bits for Alice: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
alice |= 1 << x;
else
alice |= 0 << x;
}
putchar('\n');
getchar();
printf("Put 16 bits for Betty: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
betty |= 1 << x;
else
betty |= 0 << x;
}
putchar('\n');
getchar();
printf("Put 16 bits for Carol: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
carol |= 1 << x;
else
carol |= 0 << x;
}
putchar('\n');
getchar();
bit_print(alice);
putchar('\n');
bit_print(betty);
putchar('\n');
bit_print(carol);
putchar('\n');
return 0;
}
我写这个程序是为了锻炼自己的位运算。在下面的代码行中,我尝试将值赋给三个 short
位。然后我通过 bit_print()
函数打印它们。
#include <stdio.h>
#include <limits.h>
void bit_print(short a)
{
int i;
int n = sizeof(short) * CHAR_BIT; /* in limits.h */
short mask = 1 << (n - 1); /* mask = 100...0 */
for (i = 1; i <= n; ++i) {
putchar(((a & mask) == 0) ? '0' : '1');
a <<= 1;
if (i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
}
int main(void){
short alice = 0, betty = 0, carol = 0;
int x;
char c;
printf("Put 16 bits for Alice: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
alice |= 1 << x;
else
alice |= 0 << x;
}
putchar('\n');
printf("Put 16 bits for Betty: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
betty |= 1 << x;
else
betty |= 0 << x;
}
putchar('\n');
printf("Put 16 bits for Carol: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
carol |= 1 << x;
else
carol |= 0 << x;
}
putchar('\n');
bit_print(alice);
putchar('\n');
bit_print(betty);
putchar('\n');
bit_print(carol);
putchar('\n');
return 0;
}
出于某种原因,如果我输入 1111111111111111 三次,执行程序我得到以下输出:
Put 16 bits for Alice: 1111111111111111
Put 16 bits for Betty: 1111111111111111
Put 16 bits for Carol: 1111111111111111
11111111 11111111
01111111 11111111
10111111 11111111
如您所见,最后两个变量 betty 和 carol 的最高有效位在不应该为零的地方有零。 为什么?
当使用 getchar()
时,你在输入结束时按下的换行符会被拾取,但你没有考虑它。
您需要阅读并丢弃每个循环末尾的换行符以说明这一点:
#include <stdio.h>
#include <limits.h>
void bit_print(short a)
{
int i;
int n = sizeof(short) * CHAR_BIT; /* in limits.h */
short mask = 1 << (n - 1); /* mask = 100...0 */
for (i = 1; i <= n; ++i) {
putchar(((a & mask) == 0) ? '0' : '1');
a <<= 1;
if (i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
}
int main(void){
short alice = 0, betty = 0, carol = 0;
int x;
char c;
printf("Put 16 bits for Alice: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
alice |= 1 << x;
else
alice |= 0 << x;
}
putchar('\n');
getchar();
printf("Put 16 bits for Betty: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
betty |= 1 << x;
else
betty |= 0 << x;
}
putchar('\n');
getchar();
printf("Put 16 bits for Carol: ");
for (x = 15; x >= 0; --x){
if ((c = getchar()) == '1')
carol |= 1 << x;
else
carol |= 0 << x;
}
putchar('\n');
getchar();
bit_print(alice);
putchar('\n');
bit_print(betty);
putchar('\n');
bit_print(carol);
putchar('\n');
return 0;
}