标签: bit-shift

Python 3 - 什么是“>>”

这是令人困惑的行: x_next = (x_next + (a // x_prev)) >> 1

bit-shift python-3.x

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

如何将4个2位值组合成1个8位值?

我用这个代码将4个2位值(无符号字符但它们只保存0-3的值)组合成1个单个无符号字符值

unsigned char nibble = 0;

nibble = (nibble & 0x03) | (output[i] & 0x03);
nibble = (nibble & 0x0C) | (output[i+1] & 0x03) << 2);
nibble = (nibble & 0x30) | (output[i+2] & 0x03) << 4);
nibble = (nibble & 0xC0) | (output[i+3] & 0x03) << 6);
Run Code Online (Sandbox Code Playgroud)

它会为除00 00 00 00之外的所有内容生成不正确的值(它通常会为2个不同的2位值组生成相同的结果).

我很困惑,因为上面的代码是这段代码的编辑,可以很好地将2个4位值组合成1个字节,那么为什么我的版本不能将4个2位值组合成1个字节呢?

char byte;
byte = (byte & 0xF0) | (nibble1 & 0xF); // write low quartet
byte = (byte & 0x0F) | ((nibble2 & 0xF) << …
Run Code Online (Sandbox Code Playgroud)

c c++ bit-manipulation bit-shift bit

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

了解如何使用 C 中的按位运算符计算数字的尾随零

注意- 这不是这个问题的重复 -并行计算右侧的连续零位(尾随):解释?signed()链接的问题有不同的上下文,它只询问使用的目的。不要将此问题标记为重复。

我一直在寻找一种方法来获取数字中尾随零的数量。我发现斯坦福大学在这里写了一篇有点无聊的文章,给出了以下解释。

unsigned int v;      // 32-bit word input to count zero bits on right
unsigned int c = 32; // c will be the number of zero bits on the right
v &= -signed(v);
if (v) c--;
if (v & 0x0000FFFF) c -= 16;
if (v & 0x00FF00FF) c -= 8;
if (v & 0x0F0F0F0F) c -= 4;
if (v & 0x33333333) c -= 2;
if (v & 0x55555555) c …
Run Code Online (Sandbox Code Playgroud)

c hex bit-manipulation bit-shift

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

如何在cout中使用按位移位?

我正在尝试做类似的事情:

#include <iostream>

int main()
{
    std::cout << 1 << 5 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我期望32(1左移5),但我得到15。

我正在尝试使用这样的宏:

#define BIT_SHIFT(x,y) x << y
...
cout << BIT_SHIFT(1, 5) << std::endl;
Run Code Online (Sandbox Code Playgroud)

而这发生了。

为什么?我该如何解决?

c++ cout bit-shift

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

位移行为

uint16_t a = 0x00 << 8 + 0xB9;
printf("%d",a);
Run Code Online (Sandbox Code Playgroud)

我期待185作为输出,但我得到0.

这里发生了什么?

c++ bit-shift

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

编译器如何识别字节移位运算符的长度

考虑以下行:

int mask = 1 << shift_amount;

我们知道这mask是 4 个字节,因为它是明确声明的int,但是1要移动的这个长度未知。如果编译器选择 type 为char8 位,或者它的unsigned short大小可能为16 位,那么移位结果实际上将取决于编译器关于如何处理它的决定的大小1。编译器在这里如何决定?以这种方式保留代码是否安全,或者应该改为:

int flag = 1;

int mask = flag << shift_amount;

c c++ bit-manipulation bit-shift

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

C++中涉及按位运算的表达式的值是多少

在我的机器上,以下表达式:-

int main()
{
    int q = 0b01110001;
    cout << q << endl;
    cout << (~q << 6);
}
Run Code Online (Sandbox Code Playgroud)

打印以下内容:-

113
-7296
Run Code Online (Sandbox Code Playgroud)

我试过假设 16 位整数来解决它,但我的答案与按位运算后获得的值不匹配。

这仅仅是未定义行为的情况还是我在这里遗漏了什么?

c++ bit-manipulation bit-shift

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

当无符号字符与 11100000 进行 OR 运算时,为什么按位 OR 运算无法按预期工作?

我无法理解为什么操作 'c | 11100000' 似乎不起作用。但我也注意到 'c | 10000000' 按预期工作。

#include <stdio.h>

int main()
{
    unsigned char c, c1;
    
    c = c & 0;
    c = c | 11100000;
    printf("%o \t", c);
    
    /** prints 140 ***/
    
    
    c = c & 0;
    c = c | 111;
    c << 5;
    printf("%o", c);
    
    /** prints 157 **/

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c bit-manipulation bit-shift bitwise-operators bitwise-or

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

反转数字中的位值

我想反转数字中的位值。

该方法应该按位数反转值,如下所示:

public static void main(String[] args) {

    int res = flipBit(7,1);
}

public static int flipBit(int value, int bitIndex) {

    String bin = Integer.toBinaryString(value);
    char newChar = (char) (bin.charAt(bitIndex) ^ bin.charAt(bitIndex));
    
    //pseudo code
    bin[bitIndex] = newChar;    
    
    return Integer.parseInt(bin);
}
Run Code Online (Sandbox Code Playgroud)

java integer bit-shift bitwise-operators bitwise-or

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

左移或右移带有负数的整数,这是定义的行为吗?

我尝试了下面的代码,在这两种情况下,即(左移和右移)我得到的输出为 0

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a=20;

    a=a<<-1;
    cout<<a;
    int x=20;
    x=x>>-1;
    cout<<x;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ bit-shift

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