This question already has answers here:
using Boolean to represent integers
(4 answers)
Closed 7 years ago.
int x = 5;
bool t = ((x % 3) && (x % 4));
when I try this code in visual studio, I get the value true for variable t. How this code is working?
First of all, you have the && operator, which compares two values and returns a boolean, so t will b a boolean. Secondly, in C++, every value other than 0 will evaluate to true, so x % 3, which evaluates to 2 will return true, then x % 4, which evaluates to 1 will also return true.
So in the end you have
bool t = (x % 3) && (x % 4);
which equals
bool t = 2 && 1;
which equals
bool t = true && true;
which equals
bool t = true;
All nonzero values are considered as true for logical operations like AND(&&) and OR(||)
x%3 -s a module operation returned value 2,
x%4 returns 1
2 && 1 is equal to true && true , which is equal to true.
All non zero value assignment to a bool typed variable is considered as true or 1 and all zero value assignment considered as false or 0.
Operator % returns the remainder after division (modulo) and true is any value that is not 0.
5 % 3 == 2
5 % 4 == 1
2 && 1 == true && true == true
you can just do:
bool t=(x% 3!=0) && (x% 4!=0);
Related
This question already has answers here:
Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
(5 answers)
Closed 4 years ago.
We usually use logical operators if need to combine boolean expressions. I was wondering about the expressions if don't use logical operators.
int x=101;
if(90<=x<=100)
cout<<'A';
This code still prints 'A' on console. Can you please help me to understand that how and in which order this boolean expression would be evaluated.
Since the operators have equal precedence, the expression is evaluated left-to-right :
if( (90 <= x) <= 100 )
if( (90 <= 101) <= 100 ) // substitute x's value
if( true <= 100 ) // evaluate the first <= operator
if( 1 <= 100 ) // implicit integer promotion for true to int
if( true ) // evaluate the second <= operator
To achieve the comparison you want, you would use the condition :
if( 90 <= x && x <= 100)
This is a common source of errors, because it looks right, and sytactically it is correct.
int x=101;
if(90<=x<=100)
This is equivalent to
if ( (90 <= x) <= 100)
which is
if ( true <= 100 )
and as true can convert to 1 this is
if ( true )
This expression is roughly equals to
int x=101;
bool b1 = 90 <= x; // true
bool b2 = int(b1) <= 100; // 1 <= 100 // true
if(b2)
cout<<'A';
So here is true result.
if ((pCommandPts>=tempChar.commandValue) && ((pCommandPts - tempChar.commandValue)<=0))
If pCommandPts is an int with the value 6 and tempChar.commandValue is an int with the value 3, why would this statement evaluate to false?
Left part of this expression is true in case of 6 and 3, but 6-3 is not lower or equal to zero
&& ((pCommandPts - tempChar.commandValue)<=0))
That code makes no sense, and it's almost certainly a bug.
If you rearrange the inequalities, you get:
a >= b && a <= b
Which is only true if a == b, which is not true for your case 6 != 3
This question already has answers here:
Using boolean values in C
(19 answers)
Closed 9 years ago.
I have an integer called "count" which increments when a certain condition is met. So I wanted to ask what happens if you write this condition:
if(count % 2)
{
return even_bit;
}
else
{
return odd_bit;
}
The question is basically asking if the if condition above is checking for the condition count%2 = 0 or count%2 !=0 when you don't explicitly define it in the expression for integer data type variables.
If an expression evaluates to 0, it will be interpreted as FALSE - for any non-zero value, it will be interpreted as TRUE. In other words,
if(count % 2)
is equivalent to
if(count % 2 != 0)
So your code is "backwards". If the statement is true, count is odd and you most likely would want to return the odd_bit (just guessing here.).
if(count % 2)
is equivalent to
if(count % 2 != 0)
Now it will all depend on count whether it is even or odd. In case of even, count%2 is FALSE (return odd_bit) and if count is odd then count%2 is TRUE (return even_bit).
I have an enum with 3 values:
enum InputState { Pressed, Released, Held };
And I'm using it in this code:
//GetState returns an InputState
if(myInput.GetState(keyCode) == InputState::Pressed)
{
//This means "keyCode" has the state "Pressed"
}
Why doesn't this work?
if(myInput.GetState(keyCode) == (InputState::Pressed || InputState::Held))
{
//This is always false
}
if((myInput.GetState(keyCode) == InputState::Pressed) || (myInput.GetState(keyCode) == InputState::Held))
{
//This works as intended, triggers when "keyCode" is either Pressed OR Held
}
As a test, I did:
//Using the same values from the enum, but as int now
if(1 == (1 || 2))
{
//This works as intended
}
Am I missing something?
|| is a binary operation that expects two boolean values. In your example, the boolean values are the result of testing for equality with ==.
To see why your simplified example works, let's evaluate the expression
1 == (1 || 2)
We must start inside the parentheses first, so we are going to first evaluate (1 || 2). In C++, any non-zero value is equivalent to true when it is used in a boolean expression, so (1 || 2) is equivalent to (true || true) which evaluates to true. So now our expression is
1 == true
Again, 1 is equivalent to true in this context, so this comparison is the same as true == true, which of course evaluates to true.
Yes, you are missing something. This worked by total accident:
(1 == (1 || 2))
It is NOT set comparison. It simply calculated (1 || 2) as true, then converted true to its integral value (1).
The same thing would have happened with
(1 == (1 || 4))
(1 == (0 || 1))
(1 == (0 || 4))
and they all are true.
But
(2 == (1 || 2))
is false.
Sorry for this newbie question, but I can't find on google what I need to know.
I understand return, but don't understand this... What does it mean this?
return (tail+1)%N == head%N;
Thanks a lot for patience.
It returns true or false, depending on whether the expression is true or not.
It's the same as:
if ( (tail+1)%N == head%N )
return true;
else
return false;
this
(tail+1)%N == head%N
returns a boolean value, either true or false. This statement means that after adding 1 to trail (trail + 1) and the remainder obtained after division with N is equal to remainder of head divided with N. % is used for division with remainder
(%). Modulo is the operation that gives the remainder of a division of two values.
Check this link for c++ operators : http://www.cplusplus.com/doc/tutorial/operators/
you're returning a boolean value. The value represents whether or not the remainder of (tail+1) divided by N is the same as that of head.
It evaluates the expression, and return the result. In this case it's two modulo operations that are compared, and the result is either true or false which will be returned.
Short Answer:
Because of the == operator your function will return a bool, meaning it can only be trueor false. An equivalent would be something like:
return 5 == 4;
which would return false since 5 is not equal to 4.
Long Answer:
Instead of writing this in a single line you could split it up into more lines of code. Let's just assume that tail, head and N are integer values, then you could write it like this:
int x, y;
x = (tail+1)%N;
y = head%N;
if ( x == y )
{
return true;
}
else
{
return false;
}
Now in this code there may be also that %confuses you a bit. The %is called the Modulus Operator and can give you the remainder of arithmetic operations. In a simple example this would mean:
10 % 3 = 1 because 10/3 is 3 with a remainder of 1. So to make it more clear let's just make another example with your specific problem:
Lets just assume that tail=10,head=6 and N=2. Then you would get something like this:
x = (10+1)%2
x = 11 % 2
x = 1
y = 6 % 2
y = 0
y != x
This would return false cause x and y are not equal. ( If you would run your code with the given example values )
To learn more about Modulus you can look here, or just on any other basic C++ Tutorial.
it returns true if remainder of the division for tail + 1 and head is the same
for example if tail is 2, head is 1 and N is 2
(tail + 1) % N is 1
head % N is 1 too
so whole expression returns true