473,473 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

bitwise not and "auto casts"

When the bitwise NOT operator, is placed in front of an integer
variable type (bool, char, short, int, long), the return value is a
signed int, regardless of the variable type. An example can be seen
in this code:

#include<iostream>

using namespace std;

int main(){
bool a = true;
bool b = !a;
cout << hex
<< a << endl // 1
<< b << endl // 0
<< ~a << endl // fffffffe
<< dec
<< ~a << endl // -2
<< hex
<< ~~a << endl // 1
<< ~~~a << endl; // fffffffe

cin.get();
return 0;
}

This doesn't see right...a bool that's been bit-notted *should* return
a single bit instead of returning 32-bits!! The same applies to a
bit-notted char...one would expect 8-bits in return.
Any comments
Jul 19 '05 #1
8 4569

"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9**************************@posting.google.c om...
When the bitwise NOT operator, is placed in front of an integer
variable type (bool, char, short, int, long), the return value is a
signed int, regardless of the variable type.


It is not. The type is the promoted type of the operand. It's not necessarily
a signed int. If you applied it to an unsigned, the result would be unsigned.

Bool however is promoted, just as it is for the rest of the mathematical operators.
Same thing happens with char, etc...

That's just the way C and C++ do their math.

Jul 19 '05 #2
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9**************************@posting.google.c om...
When the bitwise NOT operator, is placed in front of an integer
variable type (bool, char, short, int, long), the return value is a
signed int, regardless of the variable type.
It is not. The type is the promoted type of the operand. It's not necessarily
a signed int. If you applied it to an unsigned, the result would be unsigned.


This actually is not true with my C++ compiler. In this example, the
signed int, -2 is returned for (not 1) for bool, char, and unsigned
char. If you print the hex, you will see that it's FFFFFFFE...clearly
(~variable) returns an unsigned int regardless of variable's type.

#include<iostream>
using namespace std;
int main(){
bool a = true;
unsigned char b = 1;
signed char c = 1;
cout << ~a << endl // -2
<< ~b << endl // -2
<< ~c << endl; // -2
cin.get();
return 0;
}

And in this example, bit notting a bool returns the same initial
value!!!

#include<iostream>
using namespace std;
int main(){
bool a = 1;
bool b = ~a;
if (a == b)
cout << "imagine that, b = not a, yet a == b!!!\n";
cin.get();
return 0;
}


Bool however is promoted, just as it is for the rest of the mathematical operators.
Same thing happens with char, etc...

That's just the way C and C++ do their math.


Yep...it just seems a little nonsensical that a notted bool can return
-2...when the only possible values for a bool are 1 and 0.

I do understand though...C++ takes the value and padds with leading
zeros then does the logic on that native-length int...and returns that
value. However, in this last example where b is assigned ~a, yet b
still evaluates equal to a...this seems like an example where the c++
treatment is a poor choice. Again, I understand why it evaluates this
way...bool a = 1, ~a == fffffffe, bool b = ~a, fffffffe != 0,
therefore b == true == 1. However this does not follow any kind of
natural boolean logic. if bool a = 1, then not a should evaluate to
0, with no if's ands or buts. I realize that this behavior can be had
using the logical not rather than the bitwise not...but I still don't
like it.

My old language had a similar problem. It boiled down to something
like this...

int a = 10. int b = not a. yet a and b both evaluate to true. This
is easy to understand because both a and b hold non-zero values,
therefore both true. But with a bool type, a and not a should never
both contain non-zero values!!!

Thanks for the response.
Jul 19 '05 #3

"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9*************************@posting.google.co m...
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9**************************@posting.google.c om...
When the bitwise NOT operator, is placed in front of an integer
variable type (bool, char, short, int, long), the return value is a
signed int, regardless of the variable type.


It is not. The type is the promoted type of the operand. It's not necessarily
a signed int. If you applied it to an unsigned, the result would be unsigned.


This actually is not true with my C++ compiler. In this example, the
signed int, -2 is returned for (not 1) for bool, char, and unsigned
char. If you print the hex, you will see that it's FFFFFFFE...clearly
(~variable) returns an unsigned int regardless of variable's type.


No your tests are flawed. ostreams encoders are lousy things for determining
the TYPE of the object. Try typeid or using something that overloads over the
types you are trying to differentiate. The hex formatter always prints the number
as unsigned no matter what the input type is:

int i = -1
cout << hex << i << endl; // prints ffffffff

Second a math lesson: ~1 as a signed integer is -2, ~0 is -1 if you machine uses
2's complement math (as most do).

What happens when you apply ~ to a integral type is that the types are promoted
(as they are for the other math and bitwise operators). The following is straight
from 4.5 of the standard.

An rvalue of type char, signed char, unsigned char, short int, or unsigned short
int can be converted to an rvalue of type int if int can represent all the values of the source type; other-wise,
the source rvalue can be converted to an rvalue of type unsigned int.
An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true
becoming one.

Thus all the type conversion that you are observing happens prior to the ~ being applied.
The result of ~ is the promoted (from above) type of the argument.

None of your tests show otherwise.
Jul 19 '05 #4
hi!
If you print the hex, you will see that it's FFFFFFFE...clearly
(~variable) returns an unsigned int regardless of variable's type. ?? if the result of bit-inverting 1 is -2 then the result is not
unsigned!!?!
clearly if the MSB is 0 (positive int 2-complement) its inverted to 1
(negative)
and the other way round.
However this does not follow any kind of
natural boolean logic.

bitwise negation is not an operation of boolean logic, isn't it??? use !
instead,...

regards,
sev
Jul 19 '05 #5
"Severin Ecker" <se****@gmx.at> wrote in message news:<3f***********************@aconews.univie.ac. at>...
hi!
If you print the hex, you will see that it's FFFFFFFE...clearly
(~variable) returns an unsigned int regardless of variable's type. ?? if the result of bit-inverting 1 is -2 then the result is not
unsigned!!?!


Yes...I meant signed.
clearly if the MSB is 0 (positive int 2-complement) its inverted to 1
(negative)
and the other way round.

I understood where the -2 came from...my point was just that, for a
1-bit integer number, it's not value should also be a 1-bit number.
However this does not follow any kind of
natural boolean logic.

bitwise negation is not an operation of boolean logic, isn't it??? use !
instead,...


indeed...this is what I've done
regards,
sev


Thanks for the reply.
Jul 19 '05 #6
"Ron Natalie" <ro*@sensor.com> wrote:
The following is straight
from 4.5 of the standard.

An rvalue of type char, signed char, unsigned char, short int, or unsigned short
int can be converted to an rvalue of type int if int can represent all the values of the source type; other-wise,
the source rvalue can be converted to an rvalue of type unsigned int.
An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true
becoming one.

Thanks Ron...this was my "discovery". I think this behavior is odd,
although I can understand and predict the output once it's understood.

In either case, you seemed to be insulted by my first reply to
you...no insult was intended.

I still think that the output from this code is not at all
intuitive...

#include<iostream>
using namespace std;
int main(){
bool a = 1;
bool b = ~a;
if (a == b)
cout << "imagine that, b = not a, yet a == b!!!\n";
cin.get();
return 0;
}

Output:
imagine that, b = not a, yet a == b!!!

I'd hazard a guess that many c++ programmers who consider theselves
competent would incorrectly predict the output of this program.

Joe (a not yet competent c++ programmer) ;-)
Jul 19 '05 #7

"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9**************************@posting.google.c om...
I'd hazard a guess that many c++ programmers who consider theselves
competent would incorrectly predict the output of this program.


I don't know why. C and C++ has always promoted char's, why would bools
be any different. bools are never implmeneted as one bit storage anyhow because
they can't be smaller than char.
Jul 19 '05 #8
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9**************************@posting.google.c om...
I'd hazard a guess that many c++ programmers who consider theselves
competent would incorrectly predict the output of this program.


I don't know why. C and C++ has always promoted char's, why would bools
be any different. bools are never implmeneted as one bit storage anyhow because
they can't be smaller than char.


Fair enough...that's why I signed off as a "non-competent c++
programmer". Cheers, and thanks for the pointer to the reference that
clarifies the (non)issue I raised.

Cheers.
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field) )frequently, and I am wondering how high the number...
6
by: Bill Waddington | last post by:
Long time lurker, 1st time poster. I hope this is sufficiently c related. If not, send me to my room. I'm porting a driver to a 64-bit platform. I get handed a struct: typedef struct { ......
1
by: PL | last post by:
The Culture="auto" UICulture="auto" is a great idea, but how do you handle the situaion where SQL Server always requires a dot "." in numeric values. If I use Culture="auto"so that dates and...
1
by: Jefferds.A | last post by:
Hi, I'm putting together a little database of individuals categorized by family. I have two lists, one that contains family names and one that contains individuals' names. When you click on a link...
9
by: Beowulf | last post by:
I was having this problem: http://groups.google.com/group/microsoft.public.sqlserver.server/msg/e36e423972323378?dmode=source with it taking an inordinate amount of time to enumerate the...
22
by: nospam_news | last post by:
I currently get asked about my usage of "auto". What is it for? The keyword is clearly superflous here. In contrast to the huge majority of C/C++ developers I write definitions very explicitly...
2
by: Piotr K | last post by:
Hi, I've encountered a strange problem with Firefox which I don't have any idea how to resolve. To the point: I've <divelement with a style "height: auto" and I want to retrieve this value...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.