位模式问题 [英] Bit Pattern Problem

查看:65
本文介绍了位模式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。感谢有关qudratic方程问题的帮助......我没想到实际上在做数学......哎呀。无论如何...我是

在让以下程序工作时遇到一些麻烦。我想从基数为10的输入中输出一个位模式。

输入之后,我得到的只是零...我已查看代码但看不到问题...任何想法?

/ *显示对应于

a带符号十进制整数的位模式* /


#include< stdio.h>

#include< ; stdlib.h>


int main(无效)

{

int a,b,m,count,nbits;

无符号掩码;


/ *以位为单位确定字大小并设置初始掩码* /

nbits = 8 * sizeof(int);

m = 0x1<< (nbits - 1); / *将1放在最左边的位置* /


/ *主循环* /

做{

/ *读一个签名整数* /

printf(" \ n\\\
输入一个整数值(0表示停止):",a);

scanf("%d" ;,& a);


/ *输出位模式* /

mask = m;

for(count = 1; count< = nbits; count ++); {

b =(a& mask)? 1:0; / *打开或关闭显示位* /

printf("%x",b); / *打印显示位* /

if(count%4 == 0)

printf(""); / *第4位后的空格* /

mask>> = 1; / *移动掩码1位置向右* /

}

} while(a!= 0);

返回EXIT_SUCCESS; / *如果它在那里为什么不使用它的人! * /

}

Hi Everyone. Thanks for the help with the qudratic equation problem...I
didn''t think about actually doing the math...whoops. Anyway... I''m
having some trouble getting the following program to work. I want to
output a bit pattern from base 10 input. All I get is a zero after the
input...I''ve looked over the code but can''t see the problem...any ideas?
/* display the bit pattern corresponding to
a signed decimal integer */

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int a, b, m, count, nbits;
unsigned mask;

/* determine the word size in bits and set the initial mask */
nbits = 8 * sizeof(int);
m = 0x1 << (nbits - 1); /* Place 1 in leftmost position */

/* main loop */
do {
/* read a signed integer */
printf("\n\nEnter an integer value (0 to stop): ", a);
scanf("%d", &a);

/* output the bit pattern */
mask = m;
for(count = 1; count <= nbits; count++); {
b = (a & mask) ? 1 : 0; /* set display bit on or off */
printf("%x", b); /* print display bit */
if (count % 4 == 0)
printf(" "); /* blank space after ever 4th digit */
mask >>= 1; /* shift mask 1 position to the right */
}
} while (a != 0);
return EXIT_SUCCESS; /* If it''s there why not use it people! */
}

推荐答案

fb写道:
nbits = 8 * sizeof(int);
nbits = 8 * sizeof(int);




错误在这一行。

-

Derrick Coetzee

我将此新闻组发布到公共域。我不承担所有

明示或暗示保证并承担所有责任。我不是专业人士。



The error is on this line.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.





Derrick Coetzee写道:


Derrick Coetzee wrote:
fb写道:
fb wrote:
nbits = 8 * sizeof(int);
nbits = 8 * sizeof(int);



错误在这一行。


The error is on this line.




对 - 谢谢。



right-o thanks.


Derrick Coetzee写道:
Derrick Coetzee wrote:
fb写道:
fb wrote:
nbits = 8 * sizeof(int);

错误在这一行。
nbits = 8 * sizeof(int);

The error is on this line.




哎呀, 不,这不对。我是个白痴。实际错误在这一行:

for(count = 1; count< = nbits; count ++); {



Oops, no it''s not. I''m an idiot. The actual error is on this line:
for(count = 1; count <= nbits; count++); {




注意那些意外的分号! ^


-

Derrick Coetzee

我将此新闻组发布到公共领域。我不承担所有

明示或暗示保证并承担所有责任。我不是专业人士。



Watch out for those accidental semicolons! ^

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.


这篇关于位模式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆