boolean operator in c++ [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does 'unsigned temp:3' mean?
I saw some c++ code today that used single colons.
bool variable_name : 1;
what is the difference between this and
bool variable_name = true;

The ": 1" means it is a bit field with 1 bit, or at least that's what it means in C. It probably was put there to save some memory, allowing multiple bools to be stored in the same byte. The downside is that you probably can't make a pointer to that bool.

Related

what is the specific usecase for !! in C++ [duplicate]

This question already has answers here:
Double Negation in C++
(14 answers)
Closed 2 years ago.
Came across using !! in C++ during condition check
if ( !! (flag != 0 )){..
}
This could be directly used like
if( flag != 0 ){..
}
Is there any specific corner use case in C/C++ or is it just a form of coding style ?
In this case, it's superfluous, but in general this style is used to convert the actual expression value to
an integer type in C.
a boolean type in C++.

C++ string length [duplicate]

This question already has answers here:
What does the C++ standard state the size of int, long type to be?
(24 answers)
Getting the actual length of a UTF-8 encoded std::string?
(11 answers)
Closed 2 years ago.
I was reading the documentation of string::length and, as you can see, it says that
Returns the length of the string, in terms of bytes.
So my question is, is this ensured to be also the number of char that it contains?
I know that usually a char is 1 byte, but is this ensured somewhere? Like in the standard or somewhere else?

Comparing bool with 1 in C++ [duplicate]

This question already has answers here:
Can I assume (bool)true == (int)1 for any C++ compiler?
(5 answers)
Closed 7 years ago.
Is it correct to compare bool to 1?
In a legacy code I find often:
if (xyz.isCounterActive() == 1)
where sCounterActive() returns bool.
Obviously, if ( xyz.isCounterActive() ) is sufficient, but If I change this, I don't know which side-effects it may cause. Software is big, buggy but the customer insists, that it is working.
Compiler is VS2008
In this case result of xyz.isCounterActive() will be implicitly converted to int. There're many rules of implicit conversion, which can be found here, for example.
Probably signature of isCounterActive changed since it was introduced, and the one, who changed it, forgot to modify all isCounterActive calls.

How does C++ understand two letter characters [duplicate]

This question already has answers here:
Multicharacter literal in C and C++
(6 answers)
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 9 years ago.
C++ seems to allow up to four characters to be held in single quotes, such as:
char c = 'abcd';
but at runtime, only the last value ('d') seems to be actually stored away. This behavior seems to happen for pairs of two, three, or four (at five the compiler finally calls uncle). But what's the deal with this design? I don't really see the logic in it.

Trailing u in Hex Number C/C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Meaning of U suffix
I'm going through code that has a bunch of defines that look like:
#define HEX_NUMBER (0x000000FFu)
What is the trailing u? I've tried compiling with and without it and I don't see any difference.
Appending u to any integral constant makes the compiler interpret it as unsigned.