Confusion! I don't know what this means?

This program should give the AND of the inputted numbers.

The above statement is what I have to do to this program among putting the program in order and getting it to compile. This is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std; 

int main()
{
int i;
int k;
cout << "Enter 0 (false) or 1 (true) for the first value:\n" << endl;
cin >> i;
cout << "Enter 0 (false) or 1 (true) for the second value:\n"<< endl;
cin >> k;
cout << "AND\n" << endl;
cout << "k\t|  0\t|1" << endl;
cout << "---\t| ---\t| ---" << endl;
cout << "0\t|  0\t|0" << endl;
cout << "1\t|  0\t|  1" << endl;

if(i==1 & k==1)
	cout << "Result is TRUE" << endl;
else
	cout << "Result is FALSE" << endl;
}


Any help is much appreciated!
if(i==1 & k==1)
There is a mistake here, it should look like this :
if(i==1 && k==1)
1
2
3
if(i==1 & k==1)
There is a mistake here, it should look like this :
if(i==1 && k==1)


I noticed that as well but, it doesn't seem like it does anything... I'm not sure though?
I noticed that as well but, it doesn't seem like it does anything... I'm not sure though?


One is logical AND, one is bitwise.

Try entering, say, 2 for k and 1 for i. You'll get different results for bitwise and logical AND.
1
2
3
if(i==1 && k==1){ 
// stuff
}


The '&' will return the address of a variable, or it would be bitwise (see below), which is probably not what you want.
The '&&' operator is to use more than one thing in a condition.
Last edited on
The '&' is bitwise, which will return the address of a variable,


No no no. Bitwise does not mean "return the address of a variable". You've managed to mix up two separate operators.

& on the front of a variable, such as int* p = &a; returns the address of a variable. In this example, the address of the variable a.

& used between two variables, such as (a & b), is the bitwise AND operator. The word "bitwise" indicates that it operates on each individual bit of the two values. The word "bitwise" in no way at all means "return the address of a variable". That's just completely wrong.

Two different operations; one common symbol.


For reference, here is an example of a bitwise operation. Let's say that int a=7, and int b = 13.

c = (a & b);

a : 00000111
b : 00001101
------------------
c : 00000101

Each pair of bits is individually ANDed, giving a binary answer of 00000101, which in decimal is 5.

This code demonstrates this calculation, and for completion includes the logical AND of (a && b) as well.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main()
{
int a = 7;
int b = 13;
int c = (a & b);
int d = ( a && b);

std::cout << c << std::endl;
std::cout << d;
return 0;
}



The '&&' operator is to use more than one thing in a condition.

It is to carry out the logical AND operation. It is not some clever dodge to have a multi-part condition. That's like saying we have two feet because shoes come in pairs.
Last edited on
Topic archived. No new replies allowed.