K&安培; R - 理解练习2-8:究竟是什么在这里问? [英] K&R - Understanding exercise 2-8: Exactly what is asked here?

查看:224
本文介绍了K&安培; R - 理解练习2-8:究竟是什么在这里问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我穿过K&放大器的练习题; R书。目前我被困在练习2-8,这是说以下内容:

I'm working through the exercises in the K&R book. Currently I'm stuck at exercise 2-8, which is says the following:

写一个函数rightrot(X,N)返回整数的值X N位位​​置旋转到正确的。

我有麻烦的是,我似乎无法想象的结果应该是什么样子。

The trouble I have is that I cannot seem to picture what the result SHOULD look like.

如何或者我该怎么转动?难道我走最左边的位,并把它最右边的位置x ,在 X 移动到左侧,重复此为 N 位?或者我拿一大块( N 位),并把它放在 N 位到右侧,同时留下的休息最右边位不变?

How or what do I rotate? Do I take the leftmost bit and put it to the rightmost position of x, after x is shifted to the left and repeat this for n bits? Or do I take a chunk (n bits) and put it n bits to the right while leaving the rest of the rightmost bits unchanged?

任何有用的答案是AP preciated。谢谢你。

Any helpful answer is appreciated. Thanks.

推荐答案

旋转意味着你基本上转移到左边或右边,但否则位迷失将重新出现在另一边。

Rotating means you're essentially shifting to the left or right but the bits otherwise "lost" will reappear on the other side.

这是一个更容易与十进制数来解释:

It's a lot easier to explain with a decimal number:

旋转 123456789 由3位数字的权利将导致 789123456
旋转 123456789 来的4位数离开会导致 567891234

Rotate 123456789 to the right by 3 digits will result in 789123456. Rotate 123456789 to the left by 4 digits will result in 567891234.

所以你基本上是取n从一个侧面位,并将其附加到其他人。这是一个更容易理解,如果你认为所有坐在你围绕中心旋转的圆形或轮位。

So you'll essentially take n bits from one side and attach them to the others. It's a lot easier to understand if you think of all digits sitting on a circle or wheel you're rotating around the center.

要避免confusin只需更换旋转与移动或转移,不要忘了保存,否则失去了位。

To avoid confusin just replace "rotate" with "move" or "shift" and don't forget to save the bits otherwise lost.

这篇关于K&安培; R - 理解练习2-8:究竟是什么在这里问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆