[SOLVED] Problems with NOT ~ bitwise operator - C++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Do not treat "bool" as integer. "bool" have only two acceptable values - "true" and "false". "~" is meant to be used on integers only. For boolean variables, use "!".
Code:
bool y = true, x = true;
y = !x;
unsigned int x1 = 10, y1 = 20;
y1 = ~x1;
I thought that setting x to 1 is equivalent as declaring it true.
Not exactly. As far as I know, in this case compiler converts 1 to bool, and assigns result to variable. You can't assign "1" to boolean, since "1" is an integer.
Quote:
Originally Posted by Skyer
I was assuming I can use ~ in that manners, thank you for clearing this for me.
IF I understand it correctly, when you use ~ with bool, following happens:
bool is converted to int.
the result of ~ is calculated.
result of ~ is converted to bool
~true results in ~int(true) which is 0xfffffffe (i.e. ~1) for integer types. Which converts to "true", because it is not zero.
Code:
#include <stdio.h>
int main(int argc, char** argv){
bool a = true, b = !a, c = ~a;
int i = ~a;
printf("a: %d, b:%d, c:%d, i:%d", a, b, c, i);
return 0;
}
Please note that:
You may need to check C++ standard to check if this is a defined behavior and that it actually works the way I described (I'm not 100% sure).
Compiler may print a warning about bool to int conversion. (well, MS compiler warns you about it, g++ doesn't care)
Not exactly. As far as I know, in this case compiler converts 1 to bool, and assigns result to variable. You can't assign "1" to boolean, since "1" is an integer.
I always that that bool is just another name for an integer, and that the only real point of it is to make it more explicit that you're storing a true or false value rather than a number.
I always that that bool is just another name for an integer, and that the only real point of it is to make it more explicit that you're storing a true or false value rather than a number.
And there was me thinking that bool is a char (short int?) type where 0 = false and everything else = true; That's what I recall from my kerningham & richie book.
~ is a bitwise inverter; so if 1 is 0001 then ~1 is 1110; given that MSB is used for the signedness; you get a binary -110, which is -6 in decimal in a 4-bit signed value; so the size of the type matters for inversion.
You can use inversion nicely if you want to toggle values:
And there was me thinking that bool is a char (short int?) type where 0 = false and everything else = true; That's what I recall from my kerningham & richie book.
What do you mean? that's pretty much what I thought. I just didn't know what size of integer it is (I would assume that it's a char not to waste RAM).
Binary -110 aka 1110 is then -2, as per the OP's output and my earlier quote, not -6.
In fact 1...any number of 1s...10 is always -2 in this scheme. The same goes goes for any other negative number, see the sign extension section. Yes the MSB relates to the sign, but padding with 1s not 0s means you don't just take the rest of the bits and calculate their positive representation as though MSB were 0, then negate that.
I believe one's compliment has the behaviour you describe.
Your flags example is dealing with positive/unsigned values that are all exact powers of 2 (using a single 1 in a field of 0s). This with bitwise AND doesn't seem to relate to signedness.
And there was me thinking that bool is a char (short int?) type where 0 = false and everything else = true; That's what I recall from my kerningham & richie book.
Size of boolean is implementation defined, and although it converts to int AND many types convert to bool, I haven't found anything about the way compiler should store bool, true and false values.
For things like these C++ has bitfields and unions.
Code:
#include <stdio.h>
struct Filemask1{
int worldexec :1;
int worldwrite :1;
int worldread :1;
int groupexec :1;
int groupwrite :1;
int groupread :1;
int userexec :1;
int userwrite :1;
int userread :1;
};
struct Filemask2{
union{
struct{
unsigned int worldexec :1;
unsigned int worldwrite :1;
unsigned int worldread :1;
unsigned int groupexec :1;
unsigned int groupwrite :1;
unsigned int groupread :1;
unsigned int userexec :1;
unsigned int userwrite :1;
unsigned int userread :1;
unsigned int rsvd :23;
};
unsigned int mask;
};
};
int main(int argc, char** argv){
Filemask2 mask;
mask.mask = 0;
printf("%d\n", mask.mask);
mask.userread = 1;
printf("%d\n", mask.mask);
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.