我正在尝试像这样对 bigint 进行位移动:
let foo = BigInt(420) << 32;
Run Code Online (Sandbox Code Playgroud)
但我收到 JavaScript 错误:运算符 '<<' 无法应用于类型 'bigint' 和 'number'。
如何在 JavaScript 中对 bigint 进行位移位?
例
byte b = 127;
Run Code Online (Sandbox Code Playgroud)
(初始化为等于11111111)
现在我只关心第1位和第0位
我如何转移得到00000011
如果我是对的,我认为这被称为"掩蔽位"?
我尝试了b << 5然后b >> 5将其他位置零,但那是错误的
我的目标
switch ((myByte >> 3) & 3)
{
case 3:
resevered = true;
break;
case 2:
open = true;
break;
case 1:
SingleOnly = true;
break;
case 0:
daulMode = true;
break;
}
Run Code Online (Sandbox Code Playgroud) 你能否告诉我为什么下面的代码给出了一个否定的结果,因为无符号数的左移a有一个负值?
int main(void)
{
unsigned int a=1;
printf("%d",a<<-1);
}
Run Code Online (Sandbox Code Playgroud)
输出 -2147483648.
据我所知, 左移Float类型 不能在浮点值上使用左移运算符.但是当我尝试它时,它给出了与乘以2 n相同的答案.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float a = 1.1234;
int b = (int)(a*(1<<10));
int c = (int)(a*pow(2,10));
cout << "\n a = " << a << " b = " << b << " c = " << c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它输出a = 1.1234 b = 1150 c = 1150
在哪种情况下两个输出(b和c)会有所不同?
我正在尝试使用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位整数不是自然周期性的.
有人可以解释一下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) 当我们运行以下代码时,为什么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) 以下两个代码之间的速度有多快?为什么?在哪种情况下,一个可以优先于另一个?
double x = (a + b) >> 1
double x = (a + b) / 2.0
Run Code Online (Sandbox Code Playgroud) 抱歉天真,有人可以帮助我这个c ++程序如何工作?它应该将十进制转换为二进制.为什么在这里使用左移和右移?
void static inline unsignedToBinary(unsigned x, char*& bin)
{
bin = (char*) malloc(33);
int p = 0;
for (unsigned i = (1 << 31); i > 0; i >>= 1)
bin[p++] = ((x&i) == i) ? '1' : '0';
bin[p] = '\0';
}
Run Code Online (Sandbox Code Playgroud) 为什么-1 << 23和-1 << 55在Java中返回相同的值?它似乎应该是非常不同的,因为我正在转移更多的地方!但是,它们会产生相同的值.为什么会这样?
public class BitShifting {
public static void main(String... args) {
long foo = -1 << 23;
long bar = -1 << 55;
System.out.println(foo);
System.out.println(bar);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
-8388608
-8388608
Run Code Online (Sandbox Code Playgroud) 刚刚开始一个几乎没有背景知识的嵌入式系统课程.有人可以解释我将如何回答这个问题吗?这看起来很简单,我还无法访问我的演讲幻灯片.
有什么区别~i和INT_MAX^i
两者给出相同的没有.在二进制,但当我们打印否.输出不同,如下面的代码所示
#include <bits/stdc++.h>
using namespace std;
void binary(int x)
{
int i=30;
while(i>=0)
{
if(x&(1<<i))
cout<<'1';
else
cout<<'0';
i--;
}
cout<<endl;
}
int main() {
int i=31;
int j=INT_MAX;
int k=j^i;
int g=~i;
binary(j);
binary(i);
binary(k);
binary(g);
cout<<k<<endl<<g;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我把输出作为
1111111111111111111111111111111
0000000000000000000000000011111
1111111111111111111111111100000
1111111111111111111111111100000
2147483616
-32
Run Code Online (Sandbox Code Playgroud)
为什么k和g不同?
c++ bit-manipulation bit-shift bitwise-operators bitwise-xor
bit-shift ×12
c++ ×7
c ×2
java ×2
binary ×1
bitwise-xor ×1
c++11 ×1
database ×1
go ×1
int ×1
javascript ×1
long-integer ×1
malloc ×1
signed ×1
typescript ×1