标签: bitwise-and

在 Random.java 中,不是 x&((1L<<48)-1)==x 吗?

int next(int bits)http://developer.classpath.org/doc/java/util/Random-source.html查看。它包含行

seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
Run Code Online (Sandbox Code Playgroud)

号码(1L<<48)-11111111111111111111111111111111111111111111111111111111111111111。用数字和它做任何事情吗?这是签名多头的怪癖吗?这是旧的过时代码吗?

java random bitwise-and

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

C++ 中 !(n &amp; 1) 和 n &amp; 1 == 0 之间的区别

由于某些原因,在 C++ 中,表达式if(!(n & 1))if(n & 1 == 0)似乎不等价。

有人可以解释为什么会发生这种情况吗?

c++ bit-manipulation bit bitwise-operators bitwise-and

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

枚举标志检查标志始终返回true

我必须做一些非常愚蠢的事情,但我看不清楚是什么.在我有一个简单的控制台应用程序;

[Flags]
public enum ConsoleStates : byte
{
    TopLevel,
    All,
    MainMenu,
    SingleLeagueSelected,
}
Run Code Online (Sandbox Code Playgroud)

然后

public class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.StartUp(args);
    }

    private bool CheckFlag(ConsoleStates targetVal, ConsoleStates checkVal)
    {
        return ((targetVal & checkVal) == checkVal);
    }

    private void StartUp(string[] args)
    {
        int x = 0;
        ConsoleStates states = (ConsoleStates.All | ConsoleStates.MainMenu);
        if (CheckFlag(states, ConsoleStates.SingleLeagueSelected))
        {
            x++;
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

我的问题X最后应该为零,但它始终是1.据我所知,它应该做一点明智的操作并检查是否有单一的候选,并返回false.

这很奇怪,我所有的谷歌搜索说这很简单,只是有效,但对于我的生活,我无法得到它./羞愧地垂头丧气.

c# enums bitwise-operators bitwise-and

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

比较C++中&vs%的速度

在C++命令中是真的吗?

n&1

更快,使用更少的内存

n%2?

(其中n是int类型)

更全局,有没有办法比使用%运算符更快地找到模2的整数残差?提前致谢.

c++ performance modulo micro-optimization bitwise-and

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

按位运算产生意外输出

#include<stdio.h>
int main(void)
{
    int a=0x11;
    printf("\n %d",a);
    int b=10;
    int c=(a&b);
    printf("\t %d",c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个程序的o/p是

17 0

但是我希望程序能够对比特进行操作

17 16

为什么输出为0?

c bitwise-operators bitwise-and

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

C++ uint64_t按位并检查偶数

我有以下代码,只是检查uint64_t是否是偶数,我打算使用按位AND操作来检查,但它似乎没有工作.

这是我认为首先工作的代码:

int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
    uint64_t s,d;
    scanf("%llu %llu",&s,&d);
    //try for x
    uint64_t x;
    bool stop = false;
    x = s + d;
    printf("%llu",x&1ULL); \\ This prints 0 when the number is even but
    if(x&1ULL==0ULL){ \\ This check always returns false
        printf("%llu",x);
        x/= 2;
Run Code Online (Sandbox Code Playgroud)

如果数字是奇数或偶数,则此代码始终打印输出0或1,但if语句始终返回false.我究竟做错了什么?谢谢

c bit-manipulation bitwise-operators uint64 bitwise-and

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

为什么在 golang 中使用按位 AND 进行比较?

我正在阅读这样的一段代码(取自fsnotify):

    type Op uint32

    const (
        Create Op = 1 << iota
        Write
        Remove
        Rename
        Chmod
    )

    ...

    func (op Op) String() string {

    var buffer bytes.Buffer

    if op&Create == Create {
        buffer.WriteString("|CREATE")
    }
    if op&Remove == Remove {
        buffer.WriteString("|REMOVE")
    }
    if op&Write == Write {
        buffer.WriteString("|WRITE")
    }
    if op&Rename == Rename {
        buffer.WriteString("|RENAME")
    }
    if op&Chmod == Chmod {
        buffer.WriteString("|CHMOD")
    }
    if buffer.Len() == 0 {
        return ""
    }
    return buffer.String()[1:] 
}
Run Code Online (Sandbox Code Playgroud)

我的新手问题是为什么有人会使用按位与运算op&Remove == …

go bitwise-operators bitwise-and

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

为什么if(-8&7)返回false

当我尝试运行以下代码时,它显示“ FALSE”而不是“ TRUE”。有人可以解释为什么代码返回false吗?

#include <stdio.h>

int main(void)
{
    if(-8 & 7)
    {
        printf("TRUE");
    }
    else
    {
        printf("FALSE");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c bitwise-operators bitwise-and

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

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
查看次数

sizeof运算符在按位和在C中失败

感谢您的回应,祖%符好工作与sizeof操作符,大小也被印好的x和y的值.但是当我将所有这些说明符更改为%lld时,它应该打印实际的x和y值(因为它们很长),它会打印一些随机垃圾值.

    #include<stdio.h>

    int main()
    {
      long long x=500000000007;
      long long y=1;
      printf("size of x : %lld,x = %lld, size of y : %lld, y : %lld\n",sizeof(x),x,sizeof(y),y); 

      if(x & y)
      {
        printf("ODD IT IS :), x&1 = %lld, size of x&1 = %lld\n",x&y,sizeof(x&y) );//<--- this line
      }



      printf("After If statement ---> size of x : %lld,x = %lld\n",sizeof(x),x); 

      return 0;
    }

    Output on linux 32 bit system(Ubuntu) using gcc compiler
      size of x : 7661335479756783624,x = 34359738484, size of y : …
Run Code Online (Sandbox Code Playgroud)

c sizeof bitwise-and

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

条件检查给出错误答案

#include <stdio.h>
int main(){
  printf("%d,%d\n", 2 & (1<<1) , 2 & (1<<1)>0 );
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是2,0.

2 & (1<<1)等于 2,大于 0。那么为什么2 & (1<<1) > 0计算结果为零?

c bit-shift operator-precedence bitwise-and relational-operators

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

汇编语言中的“and”指令对操作数有何作用?

汇编语言中“and”指令的作用是什么?有人告诉我,它会检查操作数的位顺序,并将 1 设置为 true,将其他设置为 false,但我不知道它实际上做了什么,也不知道它对代码有什么影响。

assembly bitwise-operators instructions bitwise-and

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

Java 中 &amp; 的用途是什么?

所以,我正在遵循 pacman 教程,编码器使用 & 而不是 &&。但他没有解释它的作用。我搜索了一下,它说与 && 不同,& 检查这两个语句。这就说得通了。但编码器不会在比较上下文中使用它。这是他的代码。

screenData[] 是一个 255 个元素的 int 数组。

int ch = 0, pos = 0;
if((ch & 16) != 0) {
      screenData[pos] = (int) (ch & 15);
    }
Run Code Online (Sandbox Code Playgroud)

请告诉我他为什么这样做以及 & 在这种情况下有什么用处。

java bitwise-operators comparison-operators bitwise-and

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