简体   繁体   English

这里的运算符 ~ 是什么?

[英]What is the operator ~ in here?

Given:鉴于:

#define MATCHLESS_MODE          (DWORD)0x00000001   // 무적 상태
#define TRANSPARENT_MODE        (DWORD)0x00000002   // 투명 상태
#define ONEKILL_MODE            (DWORD)0x00000004   // 초필 상태
#define DONMOVE_MODE            (DWORD)0x00000008   // 움직이지 못하는 상태
#define SAYTALK_MODE            (DWORD)0x00000010   // 귓속말 못하는 상태
#define MATCHLESS2_MODE         (DWORD)0x00000020   // 무적 상태2 (데미지는 입지만 죽지는 않는다.)
#define NO_ATTACK_MODE          (DWORD)0x00000040   // 공격 못하는 상태
#define ITEM_MODE               (DWORD)0x00000080   // 아이템 못버리고 못집는 모드
#define COMMUNITY_MODE          (DWORD)0x00000100   // 길드, 파티, 친구, 개인간 거래, 개인상점 할수 없음
#define TALK_MODE               (DWORD)0x00000200   // 일반 말 못하는 상태
#define SHOUTTALK_MODE          (DWORD)0x00000400   // 외치기 못하는 상태
#define RECOVERCHAO_MODE        (DWORD)0x00000800   // 카오 극복 모드
#define FREEPK_MODE             (DWORD)0x00001000   // CTRL키 누름 없이 pk가능
#define PVPCONFIRM_MODE         (DWORD)0x00002000   // PVP거절 상태
#define QUERYSETPLAYERNAME_MODE (DWORD)0x00004000   // 캐릭터 명 변경 가능 모드
#define MODE_MAILBOX            (DWORD)0x00008000   // 읽지 않은 편지
class CFixedMode
{
private:
    DWORD m_dwMode;
public:
    CFixedMode()
    {
        m_dwMode = 0;   
    };
    ~CFixedMode(){};
    void            SetMode( DWORD dwMode )     { m_dwMode |= dwMode; }
    void            SetNotMode( DWORD dwMode )  { m_dwMode &= (~dwMode); }
    BOOL            IsMode( DWORD dwMode ) { return ( ( m_dwMode & dwMode ) == dwMode ) ? TRUE : FALSE; }
};

What does this part m_dwMode &= (~dwMode) means?这部分m_dwMode &= (~dwMode)是什么意思? I don't understand the ~看不懂~

If you mean the ~ in如果你的意思是 ~ 在

m_dwMode &= (~dwMode); 

then it's bitwise negation.那么它是按位否定。 It inverts the bits of its sole operand.它反转其唯一操作数的位。 The whole expression means - zero out the bits in m_dwMode on exactly those positions where dwMode has ones.整个表达式意味着 - 将m_dwMode的位在dwMode具有 1 的那些位置上dwMode

When you apply ~ to dwMode, it's inverted - the bits that were zero become one and vice versa.当您将 ~ 应用于 dwMode 时,它​​会被反转 - 为零的位变为 1,反之亦然。 The result is then bitwise-AND'ed with m_dwMode.然后将结果与 m_dwMode 进行按位与运算。 Now, AND with a one bit is a trivial operation.现在,带有一位的 AND 是一个微不足道的操作。 AND with a zero bit, on the other hand, yields a constant zero.另一方面,带有零位的 AND 会产生一个常数零。

Given the context, the operation is used to set and clear mode bits.给定上下文,该操作用于设置和清除模式位。 I see a bunch of bit masks in the code snippet - MATCHLESS_MODE is nothing but bit 0, TRANSPARENT_MODE is bit 1 and so on.我在代码片段中看到一堆位掩码 - MATCHLESS_MODE 只是位 0,TRANSPARENT_MODE 是位 1 等等。 So you can use SetMode() to set respective bits in the m_dwMode, and SetNotMode() to clear them.因此,您可以使用 SetMode() 设置 m_dwMode 中的相应位,并使用 SetNotMode() 清除它们。 The parameter to those functions would be one of the XXX_MODE constants, or an OR-combination of several of them.这些函数的参数将是 XXX_MODE 常量之一,或者它们中的几个的 OR 组合。

按位否定。

1001 == ~0110

It's bitwise complement .它是按位补码 All the bits of the input value are flipped.输入值的所有位都被翻转。

~dwMode is the bitwise complement, that means every bit in the DWORD is inverted. ~dwMode 是按位补码,这意味着 DWORD 中的每一位都被反转。

eg 00000000000000000000000000001010 becomes 11111111111111111111111111110101,例如 00000000000000000000000000001010 变成 11111111111111111111111111110101,

so the whole operation switches off the bits in m_dwMode which are set in dwMode所以整个操作关闭了在 dwMode 中设置的 m_dwMode 中的位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM