Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C/C+中向左旋转+;_C++_C_Bit Manipulation_Bits - Fatal编程技术网

C++ 在C/C+中向左旋转+;

C++ 在C/C+中向左旋转+;,c++,c,bit-manipulation,bits,C++,C,Bit Manipulation,Bits,可能重复: 在C/C++中,我将分别设置为左移和右移 为了测试这个操作符的功能,我想找到一个数字的第一个设定位n,左移1,然后用我之前找到的第一个位对移位后的数字进行| 我应该怎么做?long-long-int-shiftleft(long-long-int-number,无符号n) long long int shiftleft(long long int number, unsigned n) {unsigned minusone=sizeof(long long int)*8-

可能重复:

在C/C++中,我将
分别设置为左移和右移

为了测试这个操作符的功能,我想找到一个数字的第一个设定位
n
,左移1,然后用我之前找到的第一个位对移位后的数字进行
|

我应该怎么做?

long-long-int-shiftleft(long-long-int-number,无符号n)
long long int shiftleft(long long int number, unsigned n)    
{unsigned minusone=sizeof(long long int)*8-1;
 long long int r= number & (1LL<<minusone);//save the sign, which is the most significant bit
 long long int mask = (1LL<<minusone)-1;  //a mask to get all other bits

 return ( number<<n) | ((number & mask) >>(minusone-n)) | r; //rotate left in a loop only 63 bits 
}
{unsigned minusone=sizeof(long-long-int)*8-1; 长整型r=number&(1LL
long-long-int-shiftleft(长整型数,无符号n)
{unsigned minusone=sizeof(long-long-int)*8-1;

long long int r=number&(1LL您可以像这样向左旋转:

int n; // amount to rotate by
unsigned int d; // the positive number to rotate.
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n << d)|(n >> ((CHAR_BIT * sizeof(int)) - d));
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n >> d)|(n << ((CHAR_BIT * sizeof(int)) - d));
int n;//旋转的数量
unsigned int d;//要旋转的正数。
如果(n<(字符位*sizeof(int)))
int x=(n>((字符位*sizeof(int))-d));
然而,这只适用于正数。右旋转可以这样完成:

int n; // amount to rotate by
unsigned int d; // the positive number to rotate.
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n << d)|(n >> ((CHAR_BIT * sizeof(int)) - d));
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n >> d)|(n << ((CHAR_BIT * sizeof(int)) - d));
if(n<(字符位*sizeof(int)))

int x=(n>>d)|(n您可以像这样向左旋转:

int n; // amount to rotate by
unsigned int d; // the positive number to rotate.
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n << d)|(n >> ((CHAR_BIT * sizeof(int)) - d));
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n >> d)|(n << ((CHAR_BIT * sizeof(int)) - d));
int n;//旋转的数量
unsigned int d;//要旋转的正数。
如果(n<(字符位*sizeof(int)))
int x=(n>((字符位*sizeof(int))-d));
然而,这只适用于正数。右旋转可以这样完成:

int n; // amount to rotate by
unsigned int d; // the positive number to rotate.
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n << d)|(n >> ((CHAR_BIT * sizeof(int)) - d));
if(n < (CHAR_BIT * sizeof(int)))
    int x = (n >> d)|(n << ((CHAR_BIT * sizeof(int)) - d));
if(n<(字符位*sizeof(int)))

int x=(n>>d)|(n使用另一个变量作为缓冲区或使用asm。
n
的类型是什么(它很重要)在C/C++中做这件事并不难。虽然性能很重要,但是有一些指令可以做。但是你需要内部或ASM来访问它们。@ LucangGrigo:问题从来没有问过无符号,但是答案是这样的。还有一个C++版本,它的类型是Type。是否使用循环?我们只查看位模式,因此有符号/无符号是不相关的(我认为)。使用另一个变量作为缓冲区或使用asm。什么是
n
类型(它很重要)在C/C++中做这件事并不难。虽然性能很重要,但是有一些指令可以做。但是你需要内部或ASM来访问它们。@ LucangGrigo:问题从来没有问过无符号,但是答案是这样的。还有一个C++版本,它的类型是Type。旋转?我们只查看位模式,因此有符号/无符号是不相关的(我认为)。(n您在旋转中混合了n/d。将32更改为sizeof(int)。验证n(旋转量)是否不大于sizeof(int)是的,我的意思是:
CHAR\u bit*sizeof(int)
从某种意义上说,它有检查先决条件的代码。人们并不普遍认为这是一件好事。关于“不要那样做”有很多话要说。很公平。仍然不是一个好主意(n您在循环中混合了n/d。将32更改为sizeof(int)。验证n(循环量)不大于sizeof(int)是的,我的意思是:
CHAR\u BIT*sizeof(int)
嗯,“固定”的意思是,它有检查前提条件的代码。人们并不普遍认为这是一件好事。关于“不要那样做”有很多话要说。很公平。仍然不是一个好的IDEA在C中对编译器友好的循环使用的最佳做法:。对循环使用强制转换为未签名的要容易得多。对编译器友好的循环使用的最佳做法在C中是:。对循环使用强制转换为未签名的要容易得多。