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).
Related
This question already has answers here:
!! c operator, is a two NOT?
(4 answers)
Closed 10 years ago.
I have encountered the following snippet:
pt->aa[!!(ts->flags & MASK)] = -val;
What does !! (double exclamation marks/ exclamation points/ two NOT operators) stand for in c?
Doesn't (!!NULL) == NULL?
! is negation. So !! is negation of negation. What is important is the fact that the result will be an int.
!!x if x == 0 is !!0, that is !1, that is 0.
!!x if x != 0 is !!(!0), that is !!1, that is !0, that is 1.
!! is used commonly if you want to convert any non-zero value to 1 while being certain that 0 remains a 0.
And indeed, !!NULL == NULL, since !!NULL == !!0 and !!0 == !1 and finally !1 == 0.
Consequently, in the short piece of code you cited the array subscript will be either 0 if the value of the expression in parenthesis is NULL, and 1 otherwise.
It is commonly (ab)used to convert any value into the ints 0 or 1 by repeated application of the boolean not operator, !.
For instance: !56 is 0, since 56 is "true" when viewed as a boolean. This means that !!56 is 1, since !0 is 1.
!E is the same as E == 0 so !!E is the same as (E == 0) == 0. !! is used to normalize booleans values.
In C99 you can replace it by
#include <stdbool.h>
pt->aa[(bool)(ts->flags & MASK)] = -val;
Of course if your code is to be portable to C89 then you'd be better off doing the !! trick or
pt->aa[(ts->flags & MASK)!=0] = -val;
or
pt->aa[(ts->flags & MASK)?1:0] = -val;
The generated code will be certainly identical.
It converts a number into a canonical Boolean.
And note that in this case it's critical to do so, since the result is being used to index an array.
!!x is just a !(!x).
if NULL is defined as 0 then !!NULL == !!0 == !(!0) == !(1) == 0.
!! is a decent way to quiet the compiler in certain situations such as assignment in a conditional with more than one expressions, e.g:
int _blah = 100;
int *blah;
if ( _blah > 100 && !!(blah = &_blah) ) {
// do stuff
}
I don't recommend this -- warnings are usually there to enforce good coding practice.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
THEY WERE ASKING ME TO ADD SOMETHING
.I DINT KNOW WHAT TO ADD.THIS LINE IS A WASTE.
SUGGEST ME AN EDIT
#include
using namespace std;
typedef long long lli;
lli mod = 1000000007;
int n;
char a[200000 + 10];
lli dp[200000 + 10][9];
lli solve(int pos, int rem)
{
if (pos == n) //**HERE**
return (rem == 0);
if (dp[pos][rem] != -1)
return dp[pos][rem];
dp[pos][rem] = 0;
if (pos + 1 <= n)
dp[pos][rem] = solve(pos + 1, (rem * 10 + (a[pos] - '0')) % 8);
if (pos + 1 <= n)
dp[pos][rem] += solve(pos + 1, rem);
dp[pos][rem] %= mod;
return dp[pos][rem];
}
rem==0 returns either true or false ,
Eg :
rem=5;
rem=rem-5;
if(a==0)
cout<<"YES";
whereas
rem=0;
makes the rem variable have a value of 0.
rem == 0
Checks if the value of rem operand is equal to Zero or not, if yes then condition becomes true.
rem=0;
Simple Assigns value from right side to left side operand.
there is a big difference between '==' and '=' operator.
'==' is a RELATIONAL OPERATOR
It checks if the values of two operands are equal or not. If yes, then the condition becomes true else it becomes false.
POSSIBLE USE
1. In if else loops
'=' is an ASSIGNMENT OPERATOR
It assigns values from right side operands to left side operand.
POSSIBLE USE
1. During variable declaration and value assignment
The difference between the 2 is a difference between the assignment operator and the comparison operator.
rem == 0 is an example of a comparison operator, because a comparison is being made to see if the value of rem is zero or not. In this case, it will NOT set rem to zero!
If rem is equal to zero, then the following line in your code:
return (rem == 0);
is the same as
return true; // or return 1, both mean the same thing in the bool expression. It returns true as the statement is true
However if this is not the case, that rem is not equal to 0, then:
return (rem == 0);
Will be the same as:
return false; // or return 0, because the statement is false because rem is not equal to 0
Moving on to what rem = 0 does is that it simply assigns the variable on the left of the equal sign(i.e rem) the value that is on the right of the equal sign (i.e 0). This statement makes no comparison; it merely is used to assign values.
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);
This question already has an answer here:
The Definitive C++ Book Guide and List
(1 answer)
Closed 7 years ago.
I'm less than a year into C++ development (focused on other languages prior to this) and I'm looking at a guy's code who's been doing this for two decades. I've never seen this syntax before and hopefully someone can be of some help.
bool b; // There exists a Boolean variable.
int i; // There exists an integer variable.
sscanf(value, "%d", &i); // The int is assigned from a scan.
b = (i != 0); // I have never seen this syntax before.
I get that the boolean is being assigned from the int that was just scanned, but I don't get the (* != 0) aspects of what's going on. Could someone explain why this person who knows the language much better than I is doing syntax like this?
Have a read here:
http://en.cppreference.com/w/cpp/language/operator_comparison
The result of operator != is a bool. So the person is saying "compare the value in i with 0". If 'i' is not equal to 0, then the '!=' returns true.
So in effect the value in b is "true if 'i' is anything but zero"
EDIT: In response to the OP's comment on this, yes you could have a similar situation if you used any other operator which returns bool. Of course when used with an int type, the != means negative numbers evaluate to true. If > 0 were used then both 0 and negative numbers would evaluate to false.
The expression (i != 0) evaluates to a boolean value, true if the expression is true (i.e. if i is non-zero) and false otherwise.
This value is then assigned to b.
You'd get the same result from b = i;, if you prefer brevity to explicitness, due to the standard boolean conversion from numeric types which gives false for zero and true for non-zero.
Or b = (i != 0) ? true : false; if you like extraneous verbosity.
(i != 0) is an expression that evaluates to true or false. Hence, b gets the value of true/false depending on the value of i.
This is fairly fundamental syntax. The != operator performs a "not equal to" comparison.
You may be being confused by the shorthand of initialising a bool directly from the result of a comparison operator, but the syntax itself is not esoteric.
The program is essentially equivalent to:
bool b;
int i;
sscanf(value, "%d", &i);
if (i != 0)
b = true;
else
b = false;
The key is that i != 0 is itself an expression that evaluates to true or false, not some magic that may only be used in an if statement.
Basically, if the condition (i not_equal_to 0 ) is satisfied, b gets the value "true". Else b gets the value "false".
Here, "i != 0" is a boolean expression that will be true if "i" is non-zero and false if it is zero.
All that is happening here is the result of that expression is being assigned to a variable.
You could also do things like...
boolean canDrinkAlcohol = (person.age() >= 18 && person.country.equals("UK") || person.age() >= 21 && person.county.equals("US"));
...
if(canDrinkAlcohol) {
...
}
or something
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How does the C process a conditional statement such as n >= 1 <= 10?
I initially thought that it would get evaluated as n >= 1 && 1 <= 10, as it would be evaluated in Python. Since 1 <= 10 is always true, the second porition of the and is redundant (the boolean value of X && True is equivalent to the boolean value of X).
However, when I run it with n=0, the conditional gets evaluated to true. In fact, the conditional always seems to evaluate to true.
This was the example I was looking at:
if (n >= 1 <= 10)
printf("n is between 1 and 10\n");
>= operator is evaluated from left to right, so it is equal to:
if( ( n >= 1 ) <= 10)
printf("n is between 1 and 10\n");
The first ( n >= 1 ) is evaluated either as true or false, which is equal to either 1 or 0. Then that result of 1 or 0 is compared to result <= 10 which will always evaluate to true.
Thus the statement printf("n is between 1 and 10\n"); will always be printed
It's evaluated left to right like this:
n = 5;
if (n >= 1 <= 10)
// then
if (1 <= 10)
// then
if (1)
It first checks if n >= 1. If it is, it evaluates to 1, otherwise 0. This leads to the next evaluation, 1 <= 10, which evaluates to 1 as well. Note that this also succedes:
n = 5;
if (n >= 3 == 1)
Because it's evaluated like this:
n = 5;
if (n >= 3 == 1) // but you should never write code like this
// then
if (1 == 1)
// then
if (1)
Also note why it works with n = 0
n = 0;
if (n >= 1 <= 10)
// then
if (0 <= 10) // 0 isn't greater or equal to 1, so 0 (false) is "returned"
// then
if (1) // but 0 is less than or equal to 10, so it evaluates as 1 (true)