~ ¿operator?
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 =]
~ is "bitwise not". That is (in binary):
[edited by - Dean Harding on November 8, 2002 5:29:49 PM]
~01011001101
-----------
10100110010
If I had my way, I'd have all of you shot!
codeka.com - Just click it.[edited by - Dean Harding on November 8, 2002 5:29:49 PM]
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.
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.
quote:Original post by zithowaNo (unless it is a single bit). It is bitwise NOT, or 1-complement.
if(~dropped_flag_controller)
would essentially be the same thing as if
if(!dropped_flag_controller)
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]
Thanks a lot CWizard and others, glad you could help me build my genius skills, by eating some of yours.
100% n00b Gore programmer.
100% n00b Gore programmer.
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):
[edited by - CWizard on November 8, 2002 7:48:15 PM]
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]
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.
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.
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 ]
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 ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement