旋转 [英] Rotate
问题描述
您好,这是K& R的练习2-7的正确解决方案,它看起来好像是工作,但是使用sizeof(x)* 8-n来移动将要获得的已保存位
最后丢失似乎是作弊:
unsigned int rightrot(unsigned int x,int n)
{
返回〜(〜(x>> n)^((x&〜(~0<< n))<<((sizeof(x)* 8)-n )));
}
任何建议表示赞赏。
ps 写一个函数rightrot(x,n),它返回
整数x的值,向右旋转n位位置。
Hello, is this the correct solution to Exercise 2-7 of K&R, it seems to
work but using sizeof(x)*8-n to shift the saved bits that would get
lost to the end seems like cheating:
unsigned int rightrot (unsigned int x, int n)
{
return ~(~(x >> n) ^ ((x & ~(~0 << n))<<((sizeof (x)*8)-n)));
}
Any advice appreciated.
p.s. "Write a function rightrot(x,n) that returns the value of the
integer x rotated to the right by n bit positions".
推荐答案
sl ******* *********@hotmail.com 写道:
sl****************@hotmail.com writes:
unsigned int rightrot(unsigned int x,int n)
{
return~(〜(x>> n)^((x&〜(~0<< n))<<((sizeof(x)* 8)-n)));
}
ps 写一个函数rightrot(x,n),返回
整数x的值,向右旋转n位位置。
unsigned int rightrot (unsigned int x, int n)
{
return ~(~(x >> n) ^ ((x & ~(~0 << n))<<((sizeof (x)*8)-n)));
}
p.s. "Write a function rightrot(x,n) that returns the value of the
integer x rotated to the right by n bit positions".
A几条评论:
*这只能定义为n至少为0且
严格小于无符号的位数
int,因为移动了该范围之外的数量
会产生不确定的行为。
*我不明白为什么你用〜这么多,我也不知道
理解为什么^比|更受欢迎。
*一个字节中的位数可能不同于一个
实施另一个。您可以使用来自
< limits.h>的CHAR_BIT。找出每个字节的位数。
*`unsigned int''的值位数可能比其表示的
大小更少。
-
我看到它的方式,一个不同意我的聪明人是
可能是我最重要的人我将在任何给定的
日内进行互动。
--Billy Chambless
A few comments:
* This will only be well-defined for n of least 0 and
strictly less than the number of bits in an unsigned
int, because shifting by a count outside that range
yields undefined behavior.
* I don''t understand why you use ~ so much, nor do I
understand why ^ is preferred to |.
* The number of bits in a byte may vary from one
implementation to another. You can use CHAR_BIT from
<limits.h> to find out the number of bits per byte.
* An `unsigned int'' might have fewer value bits than the
size of its representation implies.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless
Ben Pfaff < bl*@cs.stanford.edu>在消息中写道
news:87 ************ @ benpfaff.org ...
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
sl **************** @ hotmail.com 写道:
sl****************@hotmail.com writes:
unsigned int rightrot(unsigned int x,int n)
{
return〜(〜(x>> n)^((x&〜( 〜0 << n))<((sizeof(x)* 8)-n)));
}
ps 写一个函数rightrot(x,n),返回
整数x的值向右旋转n位位置。
unsigned int rightrot (unsigned int x, int n)
{
return ~(~(x >> n) ^ ((x & ~(~0 << n))<<((sizeof (x)*8)-n)));
}
p.s. "Write a function rightrot(x,n) that returns the value of the
integer x rotated to the right by n bit positions".
一些注释:
*这只能定义为n至少为0且
严格小于无符号
int中的位数,因为在该范围之外移动一个计数<产生未定义的行为。
*我不明白为什么你这么用,我也不明白为什么^更喜欢|。
*字节中的位数可能因实现而异。您可以使用
< limits.h>中的CHAR_BIT。找出每个字节的位数。
*`unsigned int''的值位数可能比其表示的大小更小。
A few comments:
* This will only be well-defined for n of least 0 and
strictly less than the number of bits in an unsigned
int, because shifting by a count outside that range
yields undefined behavior.
* I don''t understand why you use ~ so much, nor do I
understand why ^ is preferred to |.
* The number of bits in a byte may vary from one
implementation to another. You can use CHAR_BIT from
<limits.h> to find out the number of bits per byte.
* An `unsigned int'' might have fewer value bits than the
size of its representation implies.
我会去的。保持严格的问题我将使用一个签名的int但是
我想负数的结果是依赖于实现的。
#include< limits .h>
int rightrot(int x,unsigned int n){
unsigned int n1 = n%WORD_BIT; / * int * /
返回的位数(x>> n1)| (x<(WORD_BIT-n1))
}
I''ll have a go. Keeping to the strict question I''ll use a signed int but
I guess the result for negative numbers is implementation dependent.
#include <limits.h>
int rightrot(int x, unsigned int n) {
unsigned int n1 = n % WORD_BIT; /* No. of bits in an int */
return (x >> n1) | (x << (WORD_BIT - n1))
}
" James Harris" < no.email.please>写道:
"James Harris" <no.email.please> writes:
我会好好的。保持严格的问题我将使用签名的int但是
我想负数的结果是依赖于实现的。
#include< limits.h>
int rightrot(int x,unsigned int n){
unsigned int n1 = n%WORD_BIT; / * int * /
返回的位数(x>> n1)| (x<(WORD_BIT - n1))
}
I''ll have a go. Keeping to the strict question I''ll use a signed int but
I guess the result for negative numbers is implementation dependent.
#include <limits.h>
int rightrot(int x, unsigned int n) {
unsigned int n1 = n % WORD_BIT; /* No. of bits in an int */
return (x >> n1) | (x << (WORD_BIT - n1))
}
不完全 - 如果n1 == 0然后向左移动WORD_BIT - n1收益
未定义的行为。
此外,x应该是未签名的。
-
如果它没有给静态这个词带来新的含义,那么它就不是一个新的C标准。
- -Peter Seebach on C99
Not quite--if n1 == 0 then shifting left by WORD_BIT - n1 yields
undefined behavior.
Also, `x'' should be unsigned.
--
"It wouldn''t be a new C standard if it didn''t give a
new meaning to the word `static''."
--Peter Seebach on C99
这篇关于旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!