Archived

This topic is now archived and is closed to further replies.

~ ¿operator?

This topic is 5514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m fairly new to c++, and I''m modifying the code for a game. I know you use someClass::~someClass() to declare a destructor, but in the code for this game i often see something like the following: void gore_CTFTeam::setDroppedController(gore_ItemControllerAPI & icapi) { dropped_flag_controller=icapi; if (~dropped_flag_controller) { // The home base flag must be made // unavailable. assert(!flag_controller.available()); track_dropped_controller=true; } else track_dropped_controller=false; } Sorry for the noobish question, but you''ve gotta start somewarez. Thanks =]

Share this post


Link to post
Share on other sites
so

if(~dropped_flag_controller)

would essentially be the same thing as if

if(!dropped_flag_controller)

and

if(!~dropped_flag_controller)

would mean the same thing as

if(dropped_flag_controller)

This is true with a bool return value, correct? So if the bits are reversed with an int, say 3, what is the result? -3?

thanks for the help.. this is used often in the code and a good thing to know.

100% n00b Gore programmer.

Share this post


Link to post
Share on other sites
quote:
Original post by zithowa
if(~dropped_flag_controller)

would essentially be the same thing as if

if(!dropped_flag_controller)
No (unless it is a single bit). It is bitwise NOT, or 1-complement.

 3 = %0011 = true
~3 = %1100 = 12 or -4 = true

 6 = %0110 = true
~6 = %1001 = 9 or -7 = true



[edited by - CWizard on November 8, 2002 5:52:39 PM]

Share this post


Link to post
Share on other sites
You're welcome, although I only clearified what the others said.

If you're interested, when negating a binary value (eg. 3 => -3) you do 2-complement, which is same as 1-complement but you add 1. Consider these signed examples (% denotes binary):
~n + 1 = -n

~0 + 1 = 15 + 1 = 16 => 0
~%0000 + 1 = %1111 + 1 = %10000 => %0000

~1 + 1 = 14 + 1 = 15 => -1
~%0001 + 1 = %1110 + 1 = %1111 => %1111

~7 + 1 = 8 + 1 = 9 => -7
~%0111 + 1 = %1000 + 1 = %1001 => %1001

~-5 + 1 = 4 + 1 = 5 => 5
~%1011 + 1 = %0100 + 1 = %0101 => %0101
EDIT: Just so you don't get it wrong here, you never do this procedure in anything higher than the chip-level, not even in assembly (for most processors, that is).

[edited by - CWizard on November 8, 2002 7:48:15 PM]

Share this post


Link to post
Share on other sites
I''m pretty sure I understand what goes on with this ~ operator (reverses bits for a given ¿value?). What I don''t understand is why anybody would ever use it.

Is there an advantage to using "if(~boolValue)" over "if(!boolValue)," or is this not a time when you would use ~?

I guess my overall question would be why would you ever use ~ for anything; what''s the point?

100% n00b Gore programmer.

Share this post


Link to post
Share on other sites
For bit field manipulations, it''s useful :
Set a bit : flags |= flag;
Clear a bit : flags &= ~flag;


In a boolean context (like your if(~dropped_flag_controller)), it''s pointless.



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites