LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2011, 10:09 AM   #1
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Rep: Reputation: 6
Problems with NOT ~ bitwise operator - C++


Hello,
I am trying to figure out this little problem with C++'s NOT bitwise operator. I guess example with code will explain my situation best:

Code:
...
bool x=1,y=0;
y=~x;
cout<<"y="<<y<<"~x"<<~x<<endl;
...
Produces output like this:
Code:
y=1~x-2
I expected y to be 0, as inversion of "1" bit is "0" bit. Also, I don't clearly understand why y is different when ~x.


Thanks for your time,

Skyer
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-23-2011, 10:24 AM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
As for 1 -> -2
Quote:
For two's complement signed integers, the bitwise complement of a number is equal to the negative of that number minus 1
 
2 members found this post helpful.
Old 09-23-2011, 10:32 AM   #3
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: 6
Thanks for your answer.
I feel embarrassed, I already read that - just didn't see the part.

Could you please provide me with the an example on how to just "toggle bits" on and off?


Thanks,

Skyer
 
Old 09-23-2011, 10:35 AM   #4
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
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;
 
2 members found this post helpful.
Old 09-23-2011, 10:51 AM   #5
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: 6
Quote:
Do not treat "bool" as integer. "bool" have only two acceptable values - "true" and "false".
I thought that setting x to 1 is equivalent as declaring it true.

Quote:
"~" is meant to be used on integers only.
I was assuming I can use ~ in that manners, thank you for clearing this for me.

I am going to try it out and report back.

EDIT: Alright, it works. Thanks for help.


Skyer

Last edited by Skyer; 09-23-2011 at 10:54 AM.
 
Old 09-23-2011, 11:59 AM   #6
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Skyer View Post
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 View Post
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:
  1. bool is converted to int.
  2. the result of ~ is calculated.
  3. 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)

Last edited by SigTerm; 09-23-2011 at 12:01 PM.
 
Old 09-23-2011, 12:20 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by SigTerm View Post
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.
 
Old 09-23-2011, 03:41 PM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by MTK358 View Post
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:
Code:
#define nothing      0
#define worldexec    1
#define worldwrite   2
#define worldread    4
#define groupexec    8
#define groupwrite  16
#define groupread   32
#define userexec    64
#define userwrite  128
#define userread   256
#define all        511

int filemask=userread|userexec|userwrite;

/* toggle userexec off */
filemask=(filemask & ~userexec);
 
Old 09-23-2011, 03:54 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Ramurd View Post
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).
 
Old 09-23-2011, 04:05 PM   #10
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Ramurd View Post
given that MSB is used for the signedness
And have negative 0? Two's complement is common for a reason.

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.

Last edited by Proud; 09-23-2011 at 05:25 PM.
 
Old 09-23-2011, 05:02 PM   #11
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Ramurd View Post
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.

Regarding your example.
Code:
#define nothing      0
#define worldexec    1
#define worldwrite   2
#define worldread    4
#define groupexec    8
#define groupwrite  16
#define groupread   32
#define userexec    64
#define userwrite  128
#define userread   256
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
Inconsistent bitwise operator behaviour in C oneandoneis2 Programming 4 02-08-2007 03:20 AM
Problems with overloading operator+ in C++ niteshadw Programming 13 07-20-2005 07:45 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM
Bitwise operators kamransoomro84 Programming 8 04-22-2004 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration