执行“AND”时出错和“或”在 c++
参考我之前的问题,我如何实现 AND 和 OR 运算
我的下一个问题是,有时它会输出一些奇怪的数字,例如 110010 & 。 010101 = 110591。为什么会发生这种情况?
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
long int s;
long int l;
long int r;
cin>>s;
cout<<endl;
cin>>l;
cout<<setfill('0')<<setw (5)<<s<<endl<<setfill('0')<<setw (5)<<l<<endl;
r=s|l;
cout<<r<<endl;
return 0;
}
With reference to my previous question, How can i implement AND and OR operations in c++
My next question is, Sometimes it outputs some weird numbers for example 110010 & 010101 = 110591. Why does that happen?
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
long int s;
long int l;
long int r;
cin>>s;
cout<<endl;
cin>>l;
cout<<setfill('0')<<setw (5)<<s<<endl<<setfill('0')<<setw (5)<<l<<endl;
r=s|l;
cout<<r<<endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您似乎正在输入数字
110010
并期望它是二进制的,因为它只有 1 和 0。该数字是一个十进制数字,您需要将其转换为二进制,以便按位运算有意义。上面是您正在使用的数字的二进制表示形式,分为四位组,因此更容易阅读。
You appear to be typing in the number
110010
and expecting that to be in binary because it only has 1s and 0s. That number is a decimal number, and you need to convert it to binary so that the bitwise operations will make sense.The above is the binary representation of the numbers you are using, divided into groups of four bits so it's easier to read.
对于您的最后一个问题:
你写的这个符号很奇怪 第一个数字被视为小数。第二个数字“010101”被视为八进制数,因为它带有“0”。当您选择的数字显示为整数时,输出应为 0。
话虽这么说,你确定你的符号正确吗?另外,当您想要按位进行 & 时,请对数字非常具体。
最后,如果您确实需要处理二进制文件,某些编译器允许使用“0b”表示法。对于其他人,我们真的很喜欢使用十六进制,即:“0x”
希望它有帮助。干杯!
For your last questions:
This notation that you've written is queer. The 1st number is treated as a decimal. The 2nd number which is '010101' is treated as an octal number as it beings with a '0'. The output should be 0 for the numbers that you've chosen when displayed as an integer.
That being said, are you sure you got your notations right? Also, be very specific with the numbers when you want to do a bit-wise &.
Lastly, if you really need to deal with binary, some compilers allow for the '0b' notation. For others, we're really comfortable with using HEX i.e.: '0x'
Hope it helps. Cheers!
使用 std::bitset 从流中获取二进制值:
运行:
Use the std::bitset to get binary values from a stream:
Running:
对于问题——如何输入二进制数而不是十进制数。
您可以将它们作为字符串读取,然后假设它们以 2 为基数进行转换。
因此,代码如下(未经测试!):
For the question -- how to input binary numbers instead of decimal.
You can read them as strings, and then convert them assuming that they are base 2.
So, code like this (not tested!):
a = b && c;
将执行逻辑和运算并将其存储到;a = b || c;
将执行逻辑或运算并将其存储到;a = b & c;
将执行按位与运算并将其存储到;a = b | c;
将执行按位或运算并将其存储到;例子:
a = b && c;
will preform logical and operation and store it into a;a = b || c;
will preform logical or operation and store it into a;a = b & c;
will preform bitwise and operation and store it into a;a = b | c;
will preform bitwise or operation and store it into a;examples :
如果您想要进行二进制运算,您的输入必须是二进制,而不是十进制或八进制。
不幸的是,C 和 C++ 似乎不支持二进制文字。有些编译器允许您使用“0b”前缀来执行它们,因此
110010 & 010101
将写成0b110010 & 0b010101
,但显然这不可移植。一种选择是使用 std::bitset,它可以让您输入二进制文件作为字符串的数字。
但是,鉴于您已经注意到这是家庭作业,我希望 std::bitset 不是您期望提供的答案,并且可以仅使用 &和|通常情况下。您看到问题的唯一原因是因为您以十进制和八进制格式输入二进制数字 - 如果您以一种格式输入和操作数字,那么就不会有问题。
If you want binary operations your inputs must be in binary, not decimal or octal.
Unfortunately C and C++ don't appear to support binary literals. Some compilers let you do them with an '0b' prefix, so
110010 & 010101
would be written0b110010 & 0b010101
, but apparently that's not portable.One option is to use a std::bitset, which lets you enter a binary number as a string.
However, given that you've noted that this is homework, I expect an std::bitset is not the answer that you are expected to provide, and can just use & and | normally. The only reason you see a problem is because you enter binary numbers in decimal and octal format - if you enter and manipulate numbers all in one format then there will be no problem.