标签: bit-shift

在javascript中的Bitshift

我有一个非常大的数字:5799218898.并希望将其右移到13位.
所以,windows-calculator或python给了我:

5799218898 >> 13 | 100010100100001110011111100001 >> 13
70791            | 10001010010000111

正如所料.

但是Javascript:

5799218898 >> 13 | 100010100100001110011111100001 >> 13
183624           | 101100110101001000

我认为这是因为javascript中的内部整数表示,但找不到任何相关内容.

javascript bit-shift

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

右移算子的奇怪行为(1 >> 32)

我最近使用右移运算符遇到了一种奇怪的行为.

以下程序:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdint.h>

int foo(int a, int b)
{
   return a >> b;
}

int bar(uint64_t a, int b)
{
   return a >> b;
}

int main(int argc, char** argv)
{
    std::cout << "foo(1, 32): " << foo(1, 32) << std::endl;
    std::cout << "bar(1, 32): " << bar(1, 32) << std::endl;
    std::cout << "1 >> 32: " << (1 >> 32) << std::endl; //warning here
    std::cout << "(int)1 >> (int)32: " << ((int)1 …
Run Code Online (Sandbox Code Playgroud)

c c++ bit-manipulation bit-shift

21
推荐指数
2
解决办法
3554
查看次数

<< =和| =的含义

在C 中<<=和是什么意思|=

我认识到<<是bithift等,但我不知道这些是什么组合.

c operators bit-shift logical-operators semantics

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

移位与在PHP中相乘

我有以下代码:

<?php
$start = 1;

$timestart = microtime(1);
for ($i = 0; $i < 1000000; $i++) {
    $result1 = $start * 4;
}
echo "\n";
echo microtime(1) - $timestart;
echo "\n";

$timestart = microtime(1);
for ($i = 0; $i < 1000000; $i++) {
    $result2 = $start << 2;
}
echo "\n";
echo microtime(1) - $timestart;
echo "\n";
Run Code Online (Sandbox Code Playgroud)

这输出:

0.14027094841003

0.12061500549316
Run Code Online (Sandbox Code Playgroud)

我在互联网上找到了一个谷歌面试问题(我想申请开发人员,但我意识到我不能),其中一个问题询问最快的方法是乘以一个数字.我的第一个想法是使用*标志,所以我测试了它.

我的问题是,为什么比乘法更快地移位?

php bit-shift multiplying

21
推荐指数
2
解决办法
1185
查看次数

在C++中,1和1i64有什么区别?

我正在将一些32位兼容代码转换为64位 - 我遇到了麻烦.我正在编译VS2008 x64项目,我收到此警告:

warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits
(was 64-bit shift intended?)
Run Code Online (Sandbox Code Playgroud)

这是原始的代码行:

if ((j & (1 << k)) != 0) {
Run Code Online (Sandbox Code Playgroud)

如果我遵循微软的建议,这就是它的样子:

if ((j & (1i64 << k)) != 0) {
Run Code Online (Sandbox Code Playgroud)

当代码将在32位和64位系统上编译时,这样做是否安全?如果是这样,请解释为什么我必须添加"i64"到底,以及为什么这不会影响32位编译.否则,将非常感谢解决方案.

除此之外,我看起来像是一个更棘手的代码.

if (id[j] != id[j ^ (1u << k)]) {
Run Code Online (Sandbox Code Playgroud)

我理解"u"意味着数字是无符号的,但是在一个不超过有符号最大值的值上指定的是什么......我猜这与位移有关?

c++ 64-bit bit-shift

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

班次计数为负数或太大错误 - 正确的解决方案?

我有以下函数用于读取big-endian四字(在抽象基本文件I/O类中):

unsigned long long File::readBigEndQuadWord(){
  unsigned long long qT = 0;
  qT |= readb() << 56;
  qT |= readb() << 48;
  qT |= readb() << 40;
  qT |= readb() << 32;
  qT |= readb() << 24;
  qT |= readb() << 16;
  qT |= readb() << 8;
  qT |= readb() << 0;
  return qT;
}
Run Code Online (Sandbox Code Playgroud)

readb()函数读取BYTE.以下是使用的typedef:

typedef unsigned char   BYTE;
typedef unsigned short  WORD;
typedef unsigned long   DWORD;
Run Code Online (Sandbox Code Playgroud)

问题是我通过shift操作在前四行获得了4个编译器警告:

警告C4293:'<<':移位计数为负数或过大,未定义的行为

我理解为什么会发生这种警告,但我似乎无法弄清楚如何正确摆脱它.我可以这样做:

qT |= (unsigned long long)readb() << 56; …

c++ math bit-shift

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

转移Java BitSet

我用a java.util.BitSet来存储密集的位向量.

我想实现一个将位向右移1的操作,类似于>>>on int.

有一个库函数可以改变BitSets吗?

如果没有,是否有比下面更好的方法?

public static void logicalRightShift(BitSet bs) {
  for (int i = 0; (i = bs.nextSetBit(i)) >= 0;) {
    // i is the first bit in a run of set bits.

    // Set any bit to the left of the run.
    if (i != 0) { bs.set(i - 1); }

    // Now i is the index of the bit after the end of the run.
    i = bs.nextClearBit(i);  // nextClearBit never …
Run Code Online (Sandbox Code Playgroud)

java bit-shift bitset

20
推荐指数
4
解决办法
9058
查看次数

为什么0乘以截断小数?

我最近发现了这段JavaScript代码:

Math.random() * 0x1000000 << 0
Run Code Online (Sandbox Code Playgroud)

我知道第一部分只是生成0到0x1000000(== 16777216)之间的随机数.

但第二部分似乎很奇怪.执行0位移位有什么意义?我不认为它会做任何事情.然而,经过进一步调查,我注意到0的偏移似乎截断了数字的小数部分.此外,无论是右移,左移,还是无符号右移,都无关紧要.

> 10.12345 << 0
10
> 10.12345 >> 0
10
> 10.12345 >>> 0
10
Run Code Online (Sandbox Code Playgroud)

我用Firefox和Chrome测试过,行为是一样的.那么,这个观察的原因是什么?它只是JavaScript的细微差别,还是也出现在其他语言中?我以为我理解了位移,但这令我感到困惑.

javascript numbers operators bit-shift bitwise-operators

20
推荐指数
3
解决办法
5713
查看次数

优化C代码

对于高性能计算课程的分配,我需要优化以下代码片段:

int foobar(int a, int b, int N)
{
    int i, j, k, x, y;
    x = 0;
    y = 0;
    k = 256;
    for (i = 0; i <= N; i++) {
        for (j = i + 1; j <= N; j++) {
            x = x + 4*(2*i+j)*(i+2*k);
            if (i > j){
               y = y + 8*(i-j);
            }else{
               y = y + 8*(j-i);
            }
        }
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

使用一些建议,我设法优化代码(或至少我认为如此),例如:

  1. 不断传播
  2. 代数简化
  3. 复制传播
  4. 常见的Subexpression消除
  5. 死代码消除
  6. 循环不变量删除
  7. 按位移位而不是乘法,因为它们更便宜.

这是我的代码:

int …
Run Code Online (Sandbox Code Playgroud)

c optimization performance bit-shift compiler-optimization

20
推荐指数
4
解决办法
3010
查看次数

Google协议缓冲区:ZigZag编码

来自编码的 "签名类型" - 协议缓冲区 - Google代码:

ZigZag编码将有符号整数映射到无符号整数,因此具有较小绝对值(例如,-1)的数字也具有较小的varint编码值.它通过正负整数来回"zig-zags"的方式做到这一点,因此-1被编码为1,1被编码为2,-2被编码为3,依此类推,就像你一样可以在下表中看到:

Signed Original  Encoded As
0                0
-1               1
1                2
-2               3
2147483647       4294967294
-2147483648      4294967295
Run Code Online (Sandbox Code Playgroud)

换句话说,使用编码每个值n

(n << 1) ^ (n >> 31)

对于sint32s,或

(n << 1) ^ (n >> 63)

对于64位版本.

如何(n << 1) ^ (n >> 31)什么表中的平等吗?我明白这对积极因素有用,但是这怎么说呢,-1?不会-1 1111 1111,(n << 1)1111 1110吗?(在任何语言中形成的负片都有点转移吗?)

尽管如此,使用公式和做(-1 << 1) ^ (-1 >> 31),假设一个32位的int,我得到1111 1111,这是40亿,而表认为我应该有1.

bit-shift protocol-buffers zigzag-encoding

19
推荐指数
2
解决办法
6523
查看次数