标签: bit-manipulation

C 中的按位运算 |=

我正在查看示例代码并发现此操作:

displayMap[x + (y/8)*LCD_WIDTH]|= 1 (shift by) shift; 
Run Code Online (Sandbox Code Playgroud)

在哪里

byte shift = y % 8;
Run Code Online (Sandbox Code Playgroud)

我理解|操作数,=但是它们两个一起做什么。

c bit-manipulation arduino bitwise-operators

-2
推荐指数
1
解决办法
3万
查看次数

为什么移位运算符似乎循环64位整数?

我正在尝试使用C++中的位数组数据结构.这是一种简单的好奇心,但我该如何解释:

uint64_t a = 1;
uint64_t b = a << 1;

cout << (a == (a << 64)) << endl; // get 1
cout << (a == (b << 63)) << endl; // get 0
Run Code Online (Sandbox Code Playgroud)

看起来像<< x是循环的时候x >= 64,但是填充用零填充x < 64.我错了吗 ?

如果没有,解释是什么?我认为64位整数不是自然周期性的.

c++ bit-manipulation bit-shift

-2
推荐指数
1
解决办法
58
查看次数

签名号码的Golang左/右移动行为

有人可以解释一下Golang中的左/右移位行为.请参考以下示例代码:https://play.golang.org/p/7vjwCbOEkw

package main

import (
    "fmt"
)

func main() {
    var lf int8 = -3
    fmt.Printf("-3 : %08b\n", lf)
    fmt.Printf("<<1: %08b\n", lf<<1)
    fmt.Printf("<<2: %08b\n", lf<<2)
    fmt.Printf("<<3: %08b\n", lf<<3)
    fmt.Printf("<<4: %08b\n", lf<<4)
    fmt.Printf("<<5: %08b, %d\n", lf<<5, lf<<5)
    fmt.Printf("<<6: %08b, %d\n", lf<<6, lf<<6)
    fmt.Printf("<<7: %08b, %d\n", lf<<7, lf<<7)
    fmt.Printf("<<8: %08b, %d\n", lf<<8, lf<<8)
    fmt.Printf("<<9: %08b, %d\n", lf<<9, lf<<9)
}

-3 : -0000011
<<1: -0000110
<<2: -0001100
<<3: -0011000
<<4: -0110000
<<5: -1100000, -96
<<6: 01000000, 64
<<7: -10000000, -128 …
Run Code Online (Sandbox Code Playgroud)

int signed bit-manipulation bit-shift go

-2
推荐指数
1
解决办法
603
查看次数

C++中1 << 31的模糊结果

当我们运行以下代码时,为什么1<<31打印18446744071562067968输出?

#include<iostream>
using namespace std;

int main(){
    unsigned long long int i = 1<<31;
    cout<<i; // this prints 18446744071562067968
}
Run Code Online (Sandbox Code Playgroud)

c++ bit-manipulation bit-shift bitwise-operators c++11

-2
推荐指数
1
解决办法
165
查看次数

奇怪的结果是c中的按位程序

写下面的代码给我奇怪的答案

#include <stdio.h>

int main (void)
{
    char a=0x03 ,b=0x01 ,x;

    printf("Enter two numbers : \n");
    scanf("%c %c",&a,&b);
    printf("0x%x 0x%x\n",a,b);
    printf("0x%x || 0x%x = 0x%x \n0x%x ^ 0x%x = 0x%x\n0x%x << 0x%x = 0x%x\n0x%x >> 0x%x = 0x%x\n",a,b,a||b,a,b,a^b,a,b,a<<b,a,b,a>>b);
    scanf("%i",x);

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

这给了我以下结果

Enter two numbers :
2 3
32 33 
0x32 || 0x33 = 0x1
0x32 ^ 0x33 = 0x1
0x32 << 0x33 = 0x1900000
0x32 >> 0x33 = 0x0
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它确实将错误的值带入a和b虽然我已经尝试使用int并且它运行良好?!

c bit-manipulation

-3
推荐指数
1
解决办法
117
查看次数

VB.NET中的按位异或

我正在尝试获取一个应用程序XOR两个十六进制值并输出结果作为十进制值.

我在互联网上搜索了一个答案,但每个结果只会给出一个真或假的结果.

我用的例子是

Value 1 = 0F8F
Value 2 = FB8E
expect result = 62465
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

vb.net bit-manipulation xor

-3
推荐指数
1
解决办法
1487
查看次数

line_c | = 064;/*错误地设置位2,4和5*/.. Misra C准则.这是怎么发生的?

line_c=0;
line_c |= 064; /* wrongly sets bits 2,4 and 5 */
line_c |= 64; /* rightly sets bit 6 */
Run Code Online (Sandbox Code Playgroud)

Misra C指南.这是怎么发生的?请解释.

c bit-manipulation misra

-3
推荐指数
2
解决办法
85
查看次数

在C++中计算(-1)^ n的有效方法

我想知道哪些是在位操作和代码长度方面计算(-1)^ n的最有效方法.

以下示例假设整数n:

int a=(n%2==0?1:-1);
int b=(n&1?-1:1);
Run Code Online (Sandbox Code Playgroud)

我不关心理解代码的简易性.

c++ bit-manipulation

-3
推荐指数
1
解决办法
101
查看次数

为什么我们不能使用整数字符串直接转换,但可以按位进行字符串?

string binary = std::bitset<16>(15).to_string();//This cast is valid
int a=5;
string s=a.to_string()//this givees error
Run Code Online (Sandbox Code Playgroud)

为什么这是无效的?to_string有什么限制吗?

c++ bit-manipulation

-3
推荐指数
1
解决办法
101
查看次数

是否有一个技巧来测试是否在int中的任何位置设置了非特定位?

我想测试是否在C中设置了任何单个位int.我不在乎它是什么位.

相当于

if (1 == count_bits (n))
Run Code Online (Sandbox Code Playgroud)

我很好奇是否有一个非循环算法,不需要特殊的硬件支持.

c bit-manipulation bit

-3
推荐指数
1
解决办法
93
查看次数

标签 统计

bit-manipulation ×10

c ×4

c++ ×4

bit-shift ×3

bitwise-operators ×2

arduino ×1

bit ×1

c++11 ×1

go ×1

int ×1

misra ×1

signed ×1

vb.net ×1

xor ×1