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<<setfil开发者_如何学JAVAl('0')<<setw (5)<<l<<endl;
r=s|l;
cout<<r<<endl;
return 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.
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.
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!
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
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);
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
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.
精彩评论