gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Using unary operators on unsigned short integer with G++

#386 - notron - Tue Jan 07, 2003 4:15 am

Okay, I know I am a Newbie and this may be stupid, but..... it appears to me that G++ (devkitadv) is expanding 16 bits to 32 bits when it evaluates unary expressions. For example, I tested the following:

unsigned short mask = 0xffff;
if ((~mask) == 0)
{ (code turns screen blue) }
else
{ (code turns screen red)}

Now, the bitwise complement of mask should be 0x0000, but the above code will turn the screen red every time. If I embed an explicit typedef override in the code like this:
if ( ( (u16)~mask) == 0)

Then it properly evaluates to TRUE !

Okay, so tell me if me what gives here?
_________________
MysticX is The Defender

#391 - anli - Tue Jan 07, 2003 9:47 am

As far as I see, you cannot avoid this promotion to take place.
The typecasting is necessary, I think.

best regards
/anli

#415 - Burre - Tue Jan 07, 2003 4:39 pm

anli wrote:
As far as I see, you cannot avoid this promotion to take place.
The typecasting is necessary, I think.

best regards
/anli


Yeah, I have to do a lot of typecasting (that shouldn't be necessary) in HAM using C++ as well. Especially when using pointers and adresses.

When using C lines like this works perfectly:
ham_StartIntHandler(INT_TYPE_VBL, &start_vblFunc);

But when using C++ it requires typecasting to (void*):
ham_StartIntHandler(INT_TYPE_VBL, (void*)&start_vblFunc);

Or else it will report an error (something like: cannot convert void (*)() to void*). Is it supposed to be that way?
_________________
"The best optimizer is between your ears..."

#448 - tom - Tue Jan 07, 2003 7:58 pm

[quote="Burre"]Or else it will report an error (something like: cannot convert void (*)() to void*). Is it supposed to be that way?[/quote]

C++ has much stronger typechecking than C, and void(*)(), a pointer to a function with no parameters and returning void is definitely not the same
as void*, so you need a typecast here.

So yes, this is supposed to be that way. Wether you regard this as a good or a bad thing is up to you =)

#456 - Burre - Tue Jan 07, 2003 8:35 pm

tom wrote:
Burre wrote:
Or else it will report an error (something like: cannot convert void (*)() to void*). Is it supposed to be that way?


C++ has much stronger typechecking than C, and void(*)(), a pointer to a function with no parameters and returning void is definitely not the same
as void*, so you need a typecast here.

So yes, this is supposed to be that way. Wether you regard this as a good or a bad thing is up to you =)


Well I don't mind really. Thanks for pointing out the difference. I'm used to C and have only coded in C++ a couple of months, so I'm not entirely used to all the differences yet. But yes I've noticed some differences in the use of explicit and implicit typeconversions.
_________________
"The best optimizer is between your ears..."