Perform XNOR on 2 integers
Solution 1:
You've gone wrong in assuming that 1101011
and 10101010
are decimal values. They are, in fact, binary.
AFAIK, C++ does not support binary assignment; if it did, you would typically indicate this with 01101011
or 01101011b
. Since it doesn't, you instead need to treat these as Hex to handle them most easily.
01101011 = 0x6b
10101010 = 0xaa
If you use these values in your code (in other words, int a = 0x6b
, etc) and then repeat your operation, you will perform the operation correctly. The way you are currently printing the result, it will be printed in decimal, which must then be converted to binary (if that's desired), though someone has offered a comment to assist you to get the output into Hex.
Solution 2:
It seems you suppose that the variables a
and b
are initialized by binary values. However these
1101011
1010101
are decimal integer literals.
If you want to use binary literals and your compiler supports them then you should write
int a = 0b1101011;
int b = 0b1010101;
Otherwise you can use the decimal integer literals but you have to apply operations to them that suitable for decimal values that to get a new decimal value that looks like a binary value.
Also it seems the operation XNOR
looks like the binary bitwise AND operator.
Below there is a demonstrative program that shows how you can get the desired rtesult.
#include <iostream>
std::ostream & binary( int x, std::ostream &os = std::cout )
{
if ( x ) binary( ( unsigned int )x >> 1 );
os << ( x & 1 );
return os;
}
int main()
{
int a = 0b1101011;
int b = 0b1010101;
int res = a & b;
binary( a ) << std::endl;
std::cout << "XNOR" << std::endl;
binary( b ) << std::endl;
std::cout << "========" << std::endl;
binary( res ) << std::endl;
return 0;
}
The program output is
01101011
XNOR
01010101
========
01000001
If you want to deal with a decimal representation of binary values then you can write a separate function for the operation XNOR
as for example
#include <iostream>
int xnor( unsigned int a, unsigned int b )
{
const unsigned int Base = 10;
unsigned int n = 1;
unsigned int res = 0;
while ( a and b )
{
res = res + n * ( a % Base and b % Base );
n *= Base;
a /= Base;
b /= Base;
}
return res;
}
int main()
{
unsigned int a = 1101011;
unsigned int b = 1010101;
int res = xnor( a, b );
std::cout << a << std::endl;
std::cout << "XNOR" << std::endl;
std::cout << b << std::endl;
std::cout << "========" << std::endl;
std::cout << res << std::endl;
return 0;
}
The program output is
1101011
XNOR
1010101
========
1000001
Take into account that your approach in any case is wrong.
This expression
res = ~(a ^ b);
sets most significant bits to 1
if they were equal to 0
for the both variables a
and b
. So for positive numbers you will always get a negative number because the sign bit will be set to 1
.
Solution 3:
I think your problem is that you want to do this:
1101011 ^
1010101
-------
= 0111110
0111110 ~
-------
= 1000001
And you actually do this:
00000000000100001100110011010011 ^ // int 1101011
00000000000011110110100110110101 // int 1010101
--------------------------------
= 00000000000111111010010101100110 // int 2073958
00000000000111111010010101100110 ~ // int 2073958
--------------------------------
= 11111111111000000101101010011001 // int -2073959
So instead of:
#include <iostream>
int main()
{
int a = 1101011;
int b = 1010101;
int res = 0;
int ans = 0;
ans = (a ^ b);
res = ~ans;
std::cout << a << '\n';
std::cout << b << '\n';
std::cout << ans << '\n';
std::cout << res << '\n';
}
Use #include <bitset>
#include <iostream>
#include <bitset>
int main()
{
int a = 107;
int b = 85;
int res = 0;
int ans = 0;
ans = (a ^ b);
res = ~ans;
std::cout << std::bitset<7>(a) << '\n';
std::cout << std::bitset<7>(b) << '\n';
std::cout << std::bitset<7>(res) << '\n';
std::cout << std::bitset<7>(res) << '\n';
}