执行“AND”时出错和“或”在 c++

发布于 2024-11-14 02:39:16 字数 618 浏览 7 评论 0原文

参考我之前的问题,我如何实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

且行且努力 2024-11-21 02:39:16

您似乎正在输入数字 110010 并期望它是二进制的,因为它只有 1 和 0。该数字是一个十进制数字,您需要将其转换为二进制,以便按位运算有意义。

 decimal                    binary
 ---------------------------------
 110010 = 0001 1010 1101 1011 1010
 010101 = 0000 0010 0111 0111 0101
 ---------------------------------
 110591 = 0001 1010 1111 1111 1111 (result of a | b)

上面是您正在使用的数字的二进制表示形式,分为四位组,因此更容易阅读。

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.

 decimal                    binary
 ---------------------------------
 110010 = 0001 1010 1101 1011 1010
 010101 = 0000 0010 0111 0111 0101
 ---------------------------------
 110591 = 0001 1010 1111 1111 1111 (result of a | b)

The above is the binary representation of the numbers you are using, divided into groups of four bits so it's easier to read.

傾旎 2024-11-21 02:39:16

对于您的最后一个问题:

我的其他问题是,,有时它
例如输出一些奇怪的数字
110010& 010101 = 110591 为什么会这样
发生

你写的这个符号很奇怪 第一个数字被视为小数。第二个数字“010101”被视为八进制数,因为它带有“0”。当您选择的数字显示为整数时,输出应为 0。

话虽这么说,你确定你的符号正确吗?另外,当您想要按位进行 & 时,请对数字非常具体。

最后,如果您确实需要处理二进制文件,某些编译器允许使用“0b”表示法。对于其他人,我们真的很喜欢使用十六进制,即:“0x”

希望它有帮助。干杯!

For your last questions:

My other questions is,,Sometimes it
output some weird numbers for example
110010 & 010101 = 110591 Why does that
happen

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!

仙气飘飘 2024-11-21 02:39:16

使用 std::bitset 从流中获取二进制值:

#include <iostream>
#include <bitset>

int main() 
{
    std::bitset<8>  val1;
    std::bitset<8>  val2;

    std::cin >> val1 >> val2;

    std::cout << val1 << " = " << val1.to_ulong() << "\n";
    std::cout << val2 << " = " << val2.to_ulong() << "\n";
    std::cout << "\n\n";

    std::cout << (val1 | val2) << " = " << (val1.to_ulong() | val2.to_ulong()) << "\n";

}

运行:

> g++ t.cpp
> ./a.out
110010
010101
00110010 = 50
00010101 = 21


00110111 = 55

Use the std::bitset to get binary values from a stream:

#include <iostream>
#include <bitset>

int main() 
{
    std::bitset<8>  val1;
    std::bitset<8>  val2;

    std::cin >> val1 >> val2;

    std::cout << val1 << " = " << val1.to_ulong() << "\n";
    std::cout << val2 << " = " << val2.to_ulong() << "\n";
    std::cout << "\n\n";

    std::cout << (val1 | val2) << " = " << (val1.to_ulong() | val2.to_ulong()) << "\n";

}

Running:

> g++ t.cpp
> ./a.out
110010
010101
00110010 = 50
00010101 = 21


00110111 = 55
榕城若虚 2024-11-21 02:39:16

对于问题——如何输入二进制数而不是十进制数。

您可以将它们作为字符串读取,然后假设它们以 2 为基数进行转换。

因此,代码如下(未经测试!):

std::string s_str;

cin >> s_str;
...
// now convert as if it's a binary string, thats what the last arg following does
s = strtol(s_str.c_str(), 0, 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!):

std::string s_str;

cin >> s_str;
...
// now convert as if it's a binary string, thats what the last arg following does
s = strtol(s_str.c_str(), 0, 2);
私藏温柔 2024-11-21 02:39:16

a = b && c; 将执行逻辑运算并将其存储到;
a = b || c; 将执行逻辑运算并将其存储到;

a = b & c; 将执行按位运算并将其存储到;
a = b | c; 将执行按位运算并将其存储到;

例子:

11 && 10 = 01
10 && 01 = 01
00 && 11 = 00

11 || 10 = 01
10 || 01 = 01
00 || 11 = 01

11  & 10 = 10
10  & 01 = 00
00  & 11 = 00

11  | 10 = 11
10  | 01 = 11
00  | 11 = 11

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 :

11 && 10 = 01
10 && 01 = 01
00 && 11 = 00

11 || 10 = 01
10 || 01 = 01
00 || 11 = 01

11  & 10 = 10
10  & 01 = 00
00  & 11 = 00

11  | 10 = 11
10  | 01 = 11
00  | 11 = 11
回忆凄美了谁 2024-11-21 02:39:16

如果您想要进行二进制运算,您的输入必须是二进制,而不是十进制或八进制。

不幸的是,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 written 0b110010 & 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文