标签: bitwise-operators

Java使用逐位运算符打印二进制数

您好我正在创建一个方法,将采取一个数字并打印它与其二进制表示.问题是我的方法打印所有0的任何正数,所有1的任何负数

private static void display( int number ){

        System.out.print(number + "\t");        
        int mask = 1 << 31;

        for(int i=1; i<=32; i++) {
            if( (mask & number) != 0 )
                System.out.print(1);
            else
                System.out.print(0);


            if( (i % 4) == 0 )
                System.out.print(" ");

        }

    }
Run Code Online (Sandbox Code Playgroud)

我知道了:这有效:

/**
     * prints the 32-bit binary representation of a number
     * @param number the number to print
     */
    private static void display( int number ){
        //display number and a tab
        System.out.print(number + "\t");

        //shift number 31 …
Run Code Online (Sandbox Code Playgroud)

java bits bitwise-operators bitwise-and

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


如何使用按位运算符"添加"两个数字?

我有两个数字,也可以是任意数字.我想将这些数字加在一起,但是采用特殊的"二进制"方式.

所以假设我有以下两个数字:79.他们的二进制形式是:

0111 = 7
1001 = 9
Run Code Online (Sandbox Code Playgroud)

我需要一种以二进制形式添加79组合的方法,但仅限于两位不同的地方.如果它们不同,则应将不同的位设置为a 1.

随着例子79,结果我要的是:

0111 = 7
1001 = 9
---- +
1111
Run Code Online (Sandbox Code Playgroud)

有没有办法在PHP中执行这样的操作?

php bitwise-operators

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

逻辑运算符与按位运算符的关键点是什么?

鉴于此声明是一种逻辑操作

 ((a > 5) && (b > 4))
Run Code Online (Sandbox Code Playgroud)

这句话是按位运算

   ((a > 5) & (b > 4)) 
Run Code Online (Sandbox Code Playgroud)

以上两个陈述并不等同.

因为(a > 5)是元素{0,1}

那么,我们为什么需要logical operators & bitwise-operation呢?

编辑:感谢所有的反馈.关于逻辑运算符的短路行为,我实际上不希望这种行为 - 我正在为GPU编写代码,其中分支会降低性能:短路导致两个分支而不是代码中的一个分支.

对于C中的数值比较,在不需要短路的情况下,似乎逻辑和按位具有相同的行为.在我的例子中,按位运算比逻辑快.

我为没有将这些细节放在原始帖子中而道歉.

c c++ bitwise-operators

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

交换给定整数中的两位

我看到了一些解决方案,但看起来很复杂。

在n,m个位置的两位之间交换的最有效方法是什么?

int swapBits(int num, int nPostion, int mPosition);
Run Code Online (Sandbox Code Playgroud)

c byte bit-manipulation bit bitwise-operators

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

为什么我们在2个数字之间使用按位运算符?

鉴于以下信息,我不明白为什么你想要评估,例如,与&运算符的2个十六进制整数.我理解下面解释的反演模式,我可以自己计算,但很难推断出为什么(从我在互联网上看到的帖子).什么是真实世界场景可能会让我在这里有所见解?

  0xff00 & 0xf0f0 is 0xf000
  0xff00 ^ 0xf0f0 is 0x0ff0
  0xff00 | 0xf0f0 is 0xfff0
For &, the result value is the bitwise AND of the operand values.
For ^, the result value is the bitwise exclusive OR of the operand values.
For |, the result value is the bitwise inclusive OR of the operand values.
Run Code Online (Sandbox Code Playgroud)

在我自己的脑海里,我现在想到这样:

if ( 65280 & 61680 )  // evaluates to 61440, not boolean?
Run Code Online (Sandbox Code Playgroud)

java bitwise-operators

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

按位运算:FitsBits

我在编写 FitsBits 很长时间时遇到了一些问题,所以我用谷歌搜索了它,并找到了一个适合测试程序的解决方案,但我无法弄清楚这意味着什么。

问题描述:

fitsBits - 如果 x 可以表示为 n 位二进制补码整数,则返回 1。
1 <= n <= 32
示例:

fitsBits(5,3) = 0, fitsBits(-4,3) = 1
合法操作:!~ & ^ | + << >> 最大操作数:15

正确的解决方案是:

int fitsBits(int x, int n)
{   
int move;  
move = 32 +(~n+1);
return !(x^((x<<move)>>move)); 
}
Run Code Online (Sandbox Code Playgroud)

但我不知道

!( x ^ ( ( x<< 移动 ) >> 移动 ) )

意味着什么。

我真的需要一些帮助。谢谢!

bitwise-operators

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

如果-8和7不为0,为什么输出为假?

如果-8和7不为0,为什么输出为假?我知道它应该是&&但问题是"代码中是否存在问题?它是什么?为什么输出为假?" 请有人帮忙.

#include <stdio.h>


int main ()
{
    if (-8 & 7) 
    {
        printf("Always\n");
    }
    else
    {
        printf("False\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c bitwise-operators

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

右移无符号字符是否填充了字符?

在我的系统上,(unsigned char) -1是(预计为 8 位字符) 1111 1111 二进制(255 十进制)。

同样,(unsigned char) -1 >> 1正如预期的 0111 1111。左边填充了一个零。

~((unsigned char) -1 >> 1) 正如预期的那样,是 1000 0000。

现在我想生成 unsigned char 0010 0000 例如。

我试过了~((unsigned char) -1 >> 1) >> 2,但这输出 1110 0000 .... 什么?为什么左边突然被填满了?


如何生成unsigned char启用第 n 位(从左侧)的 ?

我想

n   unsigned char
0   1000 0000
1   0100 0000
2   0010 0000
3   0001 0000
... 
7   0000 0001
Run Code Online (Sandbox Code Playgroud)

此刻,~((unsigned char) -1 >> 1) …

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

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

如何用另一个数字的位按位替换一个数字中的一系列位以进行递增,而不影响低位?

我有一个 16 位数字,LSB 4 位用作检查设置的位域,MSB 12 位用作number递增的 a。我知道这tempNum = (data_bits >> 4)会让我number摆脱更大的。如果我想将其增加tempNum1,然后将其放回整个 16 位数字作为替换而不影响低 4 位,我将如何进行?我只想使用bitwise操作来做到这一点。

c bit-manipulation bit bitwise-operators

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

标签 统计

bitwise-operators ×10

c ×6

bit-manipulation ×4

bit ×2

c++ ×2

java ×2

bit-shift ×1

bits ×1

bitwise-and ×1

byte ×1

php ×1