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)-1是1111111111111111111111111111111111111111111111111111111111111111。用数字和它做任何事情吗?这是签名多头的怪癖吗?这是旧的过时代码吗?
由于某些原因,在 C++ 中,表达式if(!(n & 1))和if(n & 1 == 0)似乎不等价。
有人可以解释为什么会发生这种情况吗?
我必须做一些非常愚蠢的事情,但我看不清楚是什么.在我有一个简单的控制台应用程序;
[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++命令中是真的吗?
n&1
更快,使用更少的内存
n%2?
(其中n是int类型)
更全局,有没有办法比使用%运算符更快地找到模2的整数残差?提前致谢.
#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?
我有以下代码,只是检查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.我究竟做错了什么?谢谢
我正在阅读这样的一段代码(取自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 == …
当我尝试运行以下代码时,它显示“ 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) 您好我正在创建一个方法,将采取一个数字并打印它与其二进制表示.问题是我的方法打印所有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) 感谢您的回应,祖%符好工作与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) #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
汇编语言中“and”指令的作用是什么?有人告诉我,它会检查操作数的位顺序,并将 1 设置为 true,将其他设置为 false,但我不知道它实际上做了什么,也不知道它对代码有什么影响。
所以,我正在遵循 pacman 教程,编码器使用 & 而不是 &&。但他没有解释它的作用。我搜索了一下,它说与 && 不同,& 检查这两个语句。这就说得通了。但编码器不会在比较上下文中使用它。这是他的代码。
screenData[] 是一个 255 个元素的 int 数组。
int ch = 0, pos = 0;
if((ch & 16) != 0) {
screenData[pos] = (int) (ch & 15);
}
Run Code Online (Sandbox Code Playgroud)
请告诉我他为什么这样做以及 & 在这种情况下有什么用处。