问题描述
hi group,
我尝试编译下面的代码,但我的编译器失败了。它告诉: -
printbin.c:在函数中?? ?? maina ??:
printbin.c:9:错误:无效的操作数到二进制&我无法理解它是什么意思。如何纠正错误?请帮助我只是新来的
C.
#include< stdio.h>
#include< math .h>
main()
{
int val,npow;
printf("输入值:");
scanf("%d"& val);
for(npow = 31; npow> -1; - npow){
if(val& pow(2,npow))putchar(1);
else putchar(0);
}
}
hi group,
i try to compile code below but my compiler is failing. it tells:-
printbin.c: In function a??maina??:
printbin.c:9: error: invalid operands to binary & i am not able to
understand what it mean. how to correct fault? please help i''m only new to
C.
#include<stdio.h>
#include<math.h>
main()
{
int val,npow;
printf("enter value:");
scanf("%d",&val);
for(npow=31;npow>-1;--npow){
if(val&pow(2,npow))putchar(1);
else putchar(0);
}
}
推荐答案
在其他问题中,看起来你好像想要太聪明了。 ...
将其分解为单独的步骤,而不是试图这样做
聪明把它全部塞进一条基本上不可读的线上。空白
是免费且无限制的,并且在可读性方面带来了不同的世界。
这反过来又使得尝试更容易发现问题。同上当地
变量用于中间步骤。
for(npow = ...)
{
PowerVal = pow(2,npow);
TheBit = val& PowerVal;
if(TheBit)
putchar(''1'');
else
putchar( ''0');
}
等...
其他问题之一:
" putchar(1)"将尝试打印由ASCII表示的字符
代码1,这几乎保证不会是数字1,因为你是希望
希望对于...我在押注实际出来的将是一些
不可读的图形字符,或者是Control-A的无形表示,
但是将要打印的内容将取决于平台。
您还假设int是32位长。这并不总是真实的,并且可以让你进入一个为什么这不起作用?的世界?!?!?&#;>
伤害。 ..
-
Don Bruder - - 如果你的来自:地址不在我的白名单上,
或邮件的主题不包含确切的文字PopperAndShadow
某处,发送给此的任何邮件地址将进入垃圾,没有我的
,知道它到了。对不起...< http://www.sonic.net/~dakiddfor more info
Among other problems, it looks as though you''re trying to "be too
clever"...
Break it down further into individual steps instead of trying to be so
"clever" by cramming it all on one basically unreadable line. Whitespace
is free and unlimited, and makes a world of difference in readability.
Which, in turn, makes trying to spot problems easier. Ditto local
variables for intermediate steps.
for (npow=...)
{
PowerVal = pow(2, npow);
TheBit = val & PowerVal;
if (TheBit)
putchar(''1'');
else
putchar(''0'');
}
etc...
One of the "other problems":
"putchar(1)" is going to try to print the character represented by ASCII
code 1, which is almost guaranteed to NOT be "the digit 1", as you''re
hoping for... I''m betting what actually comes out will be either some
unreadable graphic char, or an invisible representation of "Control-A",
but exactly what will get printed is going to be platform dependent.
You''re also assuming that an int is 32 bits long. Which isn''t always
true, and can land you in a world of "Why isn''t this working?!?!?"
hurt...
--
Don Bruder - da****@sonic.net - If your "From:" address isn''t on my whitelist,
or the subject of the message doesn''t contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakiddfor more info
()函数返回一个double值,二进制&运算符工作
只有整数运算符。您可以将pow()
的返回值强制转换为int,或使用更有效的方法进行位检查。你的
putchar调用应分别改为putchar(''1')和putchar(''0'')
,putchar(1)将打印字符数字
值为1而不是代表数字1的字符。
您还应该在输出结束时打印换行符。你要
应该有一个正确的main()原型和更自由的
使用空白是值得欢迎的。以下是修订版:
#include< stdio.h>
#include< math.h>
int main(无效)
{
int val,npow;
printf("输入值: ");
scanf("%d",& val);
for(npow = 31; npow -1; --npow ){
(val&(int)pow(2,npow))? putchar(''1''):putchar(''0'');
}
putchar(''\ n'');
返回0;
}
与原始版本一样,这假设一个32位的int和多次调用
pow()的效率非常低。而不是val& amp; (int)pow(2,npow)"尝试
" val& 1<< npow"。
-
Robert Gamble
The pow() function returns a double value, the binary & operator works
only with integer operators. You can cast the return value of pow()
to int, or use a more efficient method to do your bit checking. Your
putchar calls should be changed to putchar(''1'') and putchar(''0'')
respectively, putchar(1) will print the character with the numeric
value of 1 as opposed to the character that represents the number 1.
You should also print a newline at the end out your output. You
should have a correct prototype for main() and a little more liberal
use of whitespace would be welcome. Here is a revised version:
#include <stdio.h>
#include <math.h>
int main(void)
{
int val, npow;
printf("enter value: ");
scanf("%d", &val);
for (npow = 31; npow -1; --npow) {
(val & (int) pow(2, npow)) ? putchar(''1'') : putchar(''0'');
}
putchar(''\n'');
return 0;
}
Like the original, this assumes a 32-bit int and the multiple calls to
pow() are very inefficient. Instead of "val & (int) pow(2, npow)" try
"val & 1<<npow".
--
Robert Gamble
由于我们离开穿孔卡片,因此空间便宜。使用
以上将使您的代码更具可读性。
#include< stdio.h>
# include< math.h>
Since we moved away from punched cards spaces have been cheap. Using a
few more will make your code a lot more readable.
#include <stdio.h>
#include <math.h>
隐式int(你在上面使用的)已经从最新的C标准中消失了。
没有多少编译器完全支持最新的C标准,但为什么打破
兼容性只是为了节省很少的打字?还有更好的
是无明显的参数。
int main(无效)
Implicit int (which you use above) is gone from the latest C standard.
Not many compilers support the latest C standard fully, but why break
compatibility just to save a very small amount of typing? Also better to
be eplicit about no parameters.
int main(void)
由于
线路缓冲,无法保证上述内容会立即打印。你应该在这里刷新标准输出。
There is no guarantee that the above will be printed immediately due to
line buffering. You should flush stdout here.
scanf通常是用户输入功能的不良选择。最好
使用fgets,然后使用sscanf传递输入的行。无论你使用什么
,你应该检查输入函数返回的值。
scanf is generally a poor choice of function for user input. Better to
use fgets and then pass the entered line possibly using sscanf. Whatever
you use you should check the value returned by the input function.
int可能多于或少于32位。
int could be either more or less than 32 bits.
允许上面的空格,所以实际上可以读取它。
if(val& pow(2,npow)) putchar(1);
好的,&上面是唯一的一个,所以它必须是
做的事情。查找&在你的教科书中,你会发现它仅对整数类型有效。循环战争,你会发现它返回一个双$
实际上,你应该看看使用班次运算符而不是使用pow。
Lets space the above out so it is actually possible to read it.
if (val & pow(2,npow)) putchar(1);
OK, the "&" above is the only one on the line so it must be something to
do with that. Look up "&" in your text book and you will find it works
on integer types only. Loop up pow and you will find it returns a double
(i.e. something other than an integer type).
Actually, you should look at using the shift operator instead of using pow.
-
Flash Gordon
--
Flash Gordon
这篇关于打印二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!