What ! do when initializing a variable before the value [duplicate] - c++

This question already has answers here:
What is !0 in C?
(5 answers)
Closed 3 years ago.
I was doing some random stuff and I came up with int a = !3; and when I outputted the value it I was expecting an error but it got me a 0.
Why did this happen and what ! means on that example?

! is the BOOLEAN NOT operator, i.e. !true == false and !false == true. In C and C++ every value that is nonzero is treated as true when used with a boolean operator. And false is numerically 0. So 3 is treated as true and !3 = !true = false = 0.

!3 is an expression, it evaluates to a bool type.
In this example it evaluates to false.
bool's can be casted to an int, which happens automatically when you assign it to one.
The int representation of false is '0' whilst true is '1'.

That "negates" the value. Anything non-zero becomes 0, and 0 becomes 1.

Related

C++ is saying that -1 < 13 (in programming) is false [duplicate]

This question already has answers here:
Why does this if conditional with string.length() evaluate inconsistently?
(2 answers)
Signed/unsigned comparisons
(6 answers)
Weird std::string::size() in a for loop
(2 answers)
Closed 5 months ago.
I have a string called in, a string that has the value of Hello, world!. I also have a integer called i that has the value -1. When I ask C++ to print out if i is less than the length of in (in.length()), it says false but when I try -1 < 15, it says true. Why does it say false? I feel like this is extremely basic math?
string::length() returns an unsigned integer. You can't compare that to a negative signed value, so the -1 gets converted to an unsigned value, which wraps it to a very large number, which is not less than the string's length, hence the result is false.
-1 < 15, on the other hand, is comparing two signed integers, so no conversion is needed, and the result is true, as expected.

while loop with unsigned integer condition [duplicate]

This question already has answers here:
How is if statement evaluated in c++?
(7 answers)
Closed 5 years ago.
I'm not sure how the while loop works in the following simple piece of code
short CountBits(unsigned int x){
short num_bits = 0;
while (x){
num_bits += x & 1;
x >>= 1;
}
return num_bits;
}
How does an unsigned integer evaluate to True or False?
In the given context, x must be converted to true or false.
From the C++11 Standard (4.12/1):
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
Think of
while (x){ ... }
as
while (x != 0){ ... }
True is any integer which is not equal to 0. Thus if x evaluates to 0 then the loop breaks.
"How does an unsigned integer evaluate to True or False"? The same way any numeric value evaluates to true or false: 0 is false, any other value is true. Some people would write the test as while (x != 0); that's exactly the same thing.
For any integral number in C++, on most machines, 0 will evaluate to false. As such, when X becomes 0, the loop will terminate. The loop will continue while x is a non-zero value.

Why does "n&1 == 0" always return false? [duplicate]

This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Closed 6 years ago.
Why does the expression n&1 == 0 always return false, where n is an integer?
I want to use bitwise operation to determine whether n is even. However, it always return false. (The clion also prompted me that it always returns false).
What's more, it works when I use n&1 != 0 to determine whether n is odd.
Its because of the operator precedence.
== has higher precedence than the & operator, so 1 == 0 gets evaluated first to 0. Then the bit wise AND is performed which ultimately returns false.

While Loop Condition C++ [duplicate]

This question already has answers here:
How is if statement evaluated in c++?
(7 answers)
Closed 7 years ago.
What does this condition mean in a while loop?
int x;
cin >> x;
while(x) {
...
}
int has an implicit conversion to bool. Basically 0 converts to false, all nonzero values convert to true
So more verbosely, your condition would read
while (x != 0)
As #CoryKramer says, when you have a condition which only contains a variable, even if is a char, int, float, etc. the value 0 is considered as false, and any other as true. If you are using pointers is the same: the NULL value is considered as false, and any other direction is considered as true.

Why use two '!' operators on a boolean value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Double Negation in C++ code
When I scanned the Webkit source code, I found a strange use of the boolean "not" operator !:
BOOL enabled;
if (SUCCEEDED(sharedPreferences->continuousSpellCheckingEnabled(&enabled)))
continuousSpellCheckingEnabled = !!enabled;
if (SUCCEEDED(sharedPreferences->grammarCheckingEnabled(&enabled)))
grammarCheckingEnabled = !!enabled;
Why don't they use enabled directly instead of !!enabled?
It's a C++ trick to convert anything to 1 or 0.
For example, 42 is logically true, but is not 1, so applying !! to it, you turn it into 1.
It's meant to force a value to a boolean. So if the value is evaluating to something, you'll get true.
It forces a true value to be exactly 1.
In a boolean context (if (x), while (x), x ? a : b, for (; x; )), a value which is 0 means false, while any other value means true.
If your function accepts a truth value, but you need exactly 1 there, !! is fine.
In other words, !!x is the same as x ? 1 : 0.
Most probably continuousSpellCheckingEnabled is of type bool. BOOL is defined as int, so:
continuousSpellCheckingEnabled = enabled;
issues a warning, while:
continuousSpellCheckingEnabled = !!enabled;
does not.
why don't they directly use "enabled"
Double negation only cancels out for boolean values (0 and 1) so !!(0) == 0 for non-boolean values the value will be converted to boolean !!(100) == 1