其他分享
首页 > 其他分享> > c – 为什么某些情况需要使用“按位”运算符而不是“逻辑”/“相等”运算符?

c – 为什么某些情况需要使用“按位”运算符而不是“逻辑”/“相等”运算符?

作者:互联网

前几天我试图使用SDL多媒体库编写一个小的C编程,我遇到了这个小麻烦,我最终通过反复试验解决了这个问题.问题是,我理解我为解决问题所做的工作,但我并不了解问题的本质!

问题在于SDL中的键盘事件处理.处理单键按下以退出程序的代码是直截了当且简单的. [eventQueue是一个SDL_Event结构]

//checks for keypress events..
if ( eventQueue.type == SDL_KEYDOWN )
{ 
    //note: uses the boolean logical '==' equals operator..
    if ( eventQueue.key.keysym.sym == SDLK_ESCAPE )
    {
        running = false;
    }
}

在上面的代码中,只需按下ESCAPE键就可以结束主循环并导致程序清理和关闭……

但是…处理使用修饰键(shift / alt / ctrl)的按键所需的代码与’==’运算符无法正常工作.我花了很长时间才发现我需要使用按位AND运算符而不是相等(逻辑?)运算符.

//checks for keypress events..
if ( eventQueue.type == SDL_KEYDOWN )
{ 
    //note: requires the use of the bitwise AND operator..
    if (( eventQueue.key.keysym.mod & KMOD_ALT ) && (eventQueue.key.keysym.sym == SDLK_F4 ))
    {
        running = false;
    }
}

我的困惑来自这样一个事实:当使用’keysym.sym’成员时,逻辑运算符’==’工作正常,但是,当使用’keysym.mod’成员时,有必要使用’&; “按位AND运算符.

现在,如果我不得不猜测,我会说它与’keysym.sym’只需要处理代表键盘上单个键的单个数值这一事实有关,而’keysym.mod’有处理shift,ctrl和alt键的各种组合……?

总结一下我的问题:为什么会这样?除了试验和错误之外,还有什么需要学习某个数据是否需要与按位或逻辑/相等运算符进行比较?为什么“keysym.sym == SDLK_F4”工作正常,但“keysym.mod == KMOD_ALT”没有?为什么涉及十进制数的操作与比较位值的操作有不同的结果?是否还存在逻辑运算工作和按位运算不起作用的情况?

解决方法:

按位AND有点特殊. ==检查是否相等,但按位AND运算符允许您使用数字的各个位.

想象一下,您的事件被定义为键列表:

event = ['a', 'shift', 'ctrl']

然后,您可以非常轻松地检查特定修改器是否是事件的一部分:

if 'shift' in event:
  # ...

按位AND有点像in语句.您可以将事件定义为二进制数,如下所示:

event = 00010010

现在,当您执行按位AND时,您可以轻松检查是否已将某个修改器应用于事件,因为修饰符也表示为二进制数:

  00010001  # event (18)
& 00010000  # shift key (8)
----------
  00010000  # you get a non-zero answer, so the shift key is in the event
----------

  00010001  # event (18)
& 00001000  # "z" key (4)
----------
  00000000  # you get zero because the "z" key wasn't a part of the event
----------

您可以使用按位OR构造这样的事件:

  00000001  # shift key (1)
| 10100000  # "a" key (160)
----------
  10100001  # resulting event (161)
----------

维基百科很好地总结了bitwise operations

A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power/performance because of the reduced use of resources.

基本上,按位运算符允许您有效地处理存储在整数位中的信息.

标签:c,bitwise-operators,event-handling,boolean-logic,sdl
来源: https://codeday.me/bug/20190723/1512095.html