- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试实现一个向左旋转的函数,该函数将整数 x 向左旋转 n 位
到目前为止我有这个:
int rotateLeft(int x, int n) {
return ((x << n) | (x >> (32 - n)));
}
我已经意识到它不适用于有符号整数..有没有人知道如何解决这个问题?
所以现在我尝试了:
int rotateLeft(int x, int n) {
return ((x << n) | ((x >> (32 + (~n + 1))) & 0x0f));
}
并收到错误:
错误:测试 rotateLeft(-2147483648[0x80000000],1[0x1]) 失败......给出 15[0xf]。应该是 1[0x1]
最佳答案
当前对编译器友好的循环的最佳实践是 this community-wiki Q&A .来自 wikipedia 的代码不能用 clang 或早于 5.1 的 gcc 生成非常好的 asm。
在 Wikipedia 上有一个关于位旋转的非常好的、详细的解释,也就是循环移位。 .
从那里引用:
unsigned int _rotl(const unsigned int value, int shift) {
if ((shift &= sizeof(value)*8 - 1) == 0)
return value;
return (value << shift) | (value >> (sizeof(value)*8 - shift));
}
unsigned int _rotr(const unsigned int value, int shift) {
if ((shift &= sizeof(value)*8 - 1) == 0)
return value;
return (value >> shift) | (value << (sizeof(value)*8 - shift));
在您的情况下,由于您无权访问乘法运算符,因此可以替换 *8
与 << 3
.
编辑 您还可以删除 if
给出你不能使用 if
的声明的声明.这是一种优化,但没有它您仍然可以获得正确的值。
请注意,如果您真的打算在 signed
上循环位整数,旋转结果的解释将取决于平台。具体要看平台是否使用Two's Complement或 One's Complement .我想不出旋转有符号整数的位有意义的应用程序。
关于c - 按位循环左移函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10134805/
所以我正在阅读“Java:初学者指南”并且有这样的代码: class ShowBits{ int numbits; ShowBits(int n){ numbits =
我一直在调试 C++ 库中的问题,发现文字 0 和变量设置为 0 之间存在奇怪的差异,但仅在库内部。如果我将代码复制到我自己的代码中,它就会按预期工作。 库代码将 1 向下移动一个长整数,以一次屏蔽一
这个问题已经有答案了: Is left and right shifting negative integers defined behavior? (3 个回答) 已关闭 5 年前。 在解决问题时,
我想将 num 与 scale 参数相乘。这里 num 和 scale 是 float 变量。我打算使用左移 >运算符必须是整数类型。 C11:6.5 表达式(p4): Some operators
假设我有一个二进制数。 1010 是十进制的 10。 我理解左移1位本质上是数字乘以2。 教科书中有一句话让我感到困惑。 salq %cl, %rdx %rdx 是一个数字,%salq 是一个左移。我
假设我有一个二进制数。 1010 是十进制的 10。 我理解左移1位本质上是数字乘以2。 教科书中有一句话让我感到困惑。 salq %cl, %rdx %rdx 是一个数字,%salq 是一个左移。我
在这个等式中 #define mod 1000000007 int n; int num = ((1<
我必须编写一个函数来接收 像10001这样的二进制数, 和 一个十进制数,表示我应该轮类多少次。 问题是,如果我使用 C++ 运算符 << ,从后面推零,但不会丢弃第一个数字......例如 shif
标题总结如下:左移 uint64_t 不会输出预期值,我想知道为什么。我得到的是预期结果,其 4 个最高有效字节被清零。我在 Debian Jessie 64 位上使用 x86_64 CPU(Inte
System.out.println((-1<<31)); 为什么输出 -2147483648 我知道 -1<<31 会给出 10000000000000000000000000000000,所以它应
谁能解释为什么下面的代码不能编译? byte b = 255 << 1 错误: Constant value '510' cannot be converted to a 'byte' 我期待以下二进
考虑: int a = 0; a |= 1 << a; System.out.println(a); 它打印“1”。为什么?我认为向左位移 0 任意次数仍然是 0。它从哪里拉出 1?
下面是否未定义,为什么? int i = 0xFF; unsigned int r = i << 24; 最佳答案 除非int,否则该行为在技术上是未定义的类型超过 32 位。 来自 C++11,5.
我的应用由全屏 UIWebView 提供支持。它运行良好,除非调出键盘时 View 移动了大约 10 个像素,留下一个白色间隙(请参见下面的屏幕截图)。 有谁知道造成这种情况的原因以及是否有任何解决方
有人可以解释一下 Golang 中的左移/右移行为吗?请在此处引用示例代码:https://play.golang.org/p/7vjwCbOEkw package main import (
如何让 Emacs 使用 ShiftTab 将所选文本向左移动 4 个空格? 最佳答案 为此,我使用命令indent-rigidly,绑定(bind)到C-x TAB。为其指定参数 -4,将所选区域向
这个问题已经有答案了: Remove First 16 Bytes? (4 个回答) 已关闭 8 年前。 我有一个 3 字节的字节数组:byte[] VG = new Byte[3]; 这是数组的值:
假设我有一个类,我想为其重载一个基于枚举类型的运算符: #include enum class option : char { normal, do_something_stupid }; clas
我正在尝试执行以下操作。但是我不确定我可能哪里出错了 uint64_t x = (1 = width of type [-Wshift-count-overflow] 我得到输出 0。我期待像这样的二
我正在摆弄一个 Flappy Bird 的克隆,我无法弄清楚以下代码的含义 let birdCategory: UInt32 = 1 << 0 let worldCategory: UInt32 =
我是一名优秀的程序员,十分优秀!