标签: bitwise-operators

二元AND运算符的解释

有人可以解释按位,二进制 AND 运算符( & )的目的以及如何使用它?我正在研究创建isprime函数的不同方法并遇到了这个问题。

def isprime(n):
    # make sure n is a positive integer
    n = abs(int(n))
    # 0 and 1 are not primes
    if n < 2:
        return False
    # 2 is the only even prime number
    if n == 2: 
        return True    
    # all other even numbers are not primes
    if not n & 1: 
        return False
    # range starts with 3 and only needs to go up the squareroot of n
    # for all …
Run Code Online (Sandbox Code Playgroud)

python bitwise-operators

0
推荐指数
1
解决办法
1462
查看次数

创建 32 位位掩码的最有效方法

我想创建一个掩码来设置第一个nnybbles的位,以及m这些 nybbles的第一个位, wheren <=8m <=4 .

高效,我的意思是一种最小化按位运算的方法。

目前我使用蛮力方法:首先创建一个 nybble 掩码,然后继续左移掩码并将这些数字组合在一起。

这是我目前的方法:

#define NIBBLE   ((unsigned int)0xF >> m))
#define MASK     ((NIBBLE | (NIBBLE << 4) |  (NIBBLE << (8)) | (NIBBLE << (12)) | (NIBBLE << (16)) | (NIBBLE << (20)) | (NIBBLE << (24)) | (NIBBLE << (28)))  >> (n*4) )
Run Code Online (Sandbox Code Playgroud)

c bit-manipulation bitwise-operators

0
推荐指数
1
解决办法
930
查看次数

C++ 含义 |= 和 &amp;=

我有一部分代码包含以下功能:

void Keyboard(int key)
{
    switch (key) {
    case GLFW_KEY_A: m_controlState |= TDC_LEFT; break;
    case GLFW_KEY_D: m_controlState |= TDC_RIGHT; break;
    case GLFW_KEY_W: m_controlState |= TDC_UP; break;
    case GLFW_KEY_S: m_controlState |= TDC_DOWN; break;
    default: Test::Keyboard(key);
    }
}

void KeyboardUp( int key)
{
    switch (key) {
    case GLFW_KEY_A: m_controlState &= ~TDC_LEFT; break;
    case GLFW_KEY_D: m_controlState &= ~TDC_RIGHT; break;
    case GLFW_KEY_W: m_controlState &= ~TDC_UP; break;
    case GLFW_KEY_S: m_controlState &= ~TDC_DOWN; break;
    default: Test::Keyboard(key);
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道开关盒是什么,但我不明白这些部件的作用。

m_controlState |= TDC_LEFT
m_controlState &= ~TDC_LEFT
Run Code Online (Sandbox Code Playgroud)

m_controlState …

c++ bit-manipulation bitwise-operators box2d

0
推荐指数
2
解决办法
8601
查看次数

Java 按位和运算符多重权限

我正在开发一个 java web 应用程序,在一个数据库表中,我有一列类型为 number。但该列用于存储多个值。(即许可如下)

  1. 没有 = 1;
  2. 视图 = 2;
  3. 添加 = 4;
  4. 编辑 = 8;
  5. 插入 = 16;
  6. 删除 = 32;
  7. 全部 = 64;

问题

  1. 如果该列的值为 3 --> 那么我不需要选择任何内容,查看权限。
  2. 如果该列的值为 12 --> 那么我需要选择添加、编辑。

像这样的东西

我不明白我们可以通过按位运算符来做到这一点。任何实现这一点的代码都会很棒。

java bitwise-operators

0
推荐指数
1
解决办法
1195
查看次数

仅使用按位运算符复制 for 循环的功能

我正在尝试仅使用按位和某些运算符(包括 ! ~ & ^ | + << >>

int loop(int x) {
   for (int i = 1; i < 32; i += 2)
     if ((x & (1 << i)) == 0)
       return 0;
   return 1; 
}
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何仅使用这些运算符来复制循环的累积性质。我明白移位<< >>会让我乘以除以。然而,操作使用! ~ & ^ ~已被证明更加困难。有小费吗?

http://www.tutorialspoint.com/cprogramming/c_operators.htm

编辑:我了解如何添加位,但不知道如何在不先调用 while 或 for 循环的情况下实现这样的输出。

c c++ for-loop bit-manipulation bitwise-operators

0
推荐指数
1
解决办法
2462
查看次数

C:在O(1)中将整数的第i位设置为1

现在,我知道设置数字的第i位的方法是使用移位运算符移位1直到达到所需位,然后只是或者数字.但是这个过程是O(数字的长度),因为将数字移到第i个位置就像遍历到那里,对吧?如果我错了,请纠正我.

这是我的代码:

x = x| (1<<i)
Run Code Online (Sandbox Code Playgroud)

有没有办法在O(1)中做到这一点?换句话说,如何直接访问数字中的位?我正在考虑数组索引.

c bit-manipulation bit bitwise-operators time-complexity

0
推荐指数
1
解决办法
142
查看次数

Discord 按位权限计算器

我试图让我的工具/脚本确定登录用户是否具有使用该工具的正确权限。但是 Discord 以 int 权限进行响应。

似乎我必须使用按位运算符检查权限,但是从我用谷歌搜索或搜索的几乎所有内容来看,我发现的所有内容似乎完全无关紧要。

示例权限: 2146958591

权限参考;

// General generalCreateInstantInvite: 0x1, generalKickMembers: 0x2, generalBanMembers: 0x4, generalAdministrator: 0x8, generalManageChannels: 0x10, generalManageServer: 0x20, generalChangeNickname: 0x4000000, generalManageNicknames: 0x8000000, generalManageRoles: 0x10000000, generalManageWebhooks: 0x20000000, generalManageEmojis: 0x40000000, generalViewAuditLog: 0x80, // Text textAddReactions: 0x40, textReadMessages: 0x400, textSendMessages: 0x800, textSendTTSMessages: 0x1000, textManageMessages: 0x2000, textEmbedLinks: 0x4000, textAttachFiles: 0x8000, textReadMessageHistory: 0x10000, textMentionEveryone: 0x20000, textUseExternalEmojis: 0x40000, // Voice voiceViewChannel: 0x400, voiceConnect: 0x100000, voiceSpeak: 0x200000, voiceMuteMembers: 0x400000, voiceDeafenMembers: 0x800000, voiceMoveMembers: 0x1000000, voiceUseVAD: 0x2000000 是否有我可以研究的工具或示例脚本来确定如何进行此计算?

先感谢您!

php api bitwise-operators discord

0
推荐指数
2
解决办法
2174
查看次数

为什么 10101 | 11100 在 JavaScript 中返回 12157?

我怎么做的:10101 | 11100 我得到 12157?我只想比较这些位以获得 11101。

javascript bitwise-operators

0
推荐指数
1
解决办法
28
查看次数

按位运算产生意想不到的结果

我试图理解逻辑操作数and和按位操作数之间的区别&。于是我写了两条语句,得到了意想不到的结果。我理解第一个,并希望第二个会给我与第一个相同的结果。

  1. 第一个我得到 False 这正是我想要的。但是第二个我得到了 True 这让我感到困惑。

  2. 既然3%3 == 0归来True,又3%5 == 0归来False。并True & False给我False。为什么,当我把它们放在一起时,我得到了True?有人可以解释为什么第二个给我True而不是False吗?

    3 % 3 == 0 and 3 % 5 == 0
    3 % 3 == 0 & 3 % 5 ==0 
    3 % 3 == 0
    3 % 5 == 0
    True & False
    
    Run Code Online (Sandbox Code Playgroud)

python bitwise-operators

0
推荐指数
1
解决办法
66
查看次数

为什么 True &amp; True 给出 False?

>>> 5 > 4 & 6 > 5
Run Code Online (Sandbox Code Playgroud)

为什么上面的表达式False在 Python 中给出,if 5 > 4isTrue6 > 5is also True

python bitwise-operators

0
推荐指数
1
解决办法
54
查看次数