我在哪里可以找到对C有点移位引导? [英] Where can I find a bit shifting guide for C?

查看:123
本文介绍了我在哪里可以找到对C有点移位引导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过<一个href=\"http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting\">http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting但我仍然觉得有点概念转变不难理解了。

有人能指出我在一个更基本指南位转移的C.我希望这将是东西方向很长,因为它需要覆盖整个主题。

我真的不明白,但我想了解它,所以任何帮助将是AP preciated。

我学习了K&再现性,而这正是这是这样我就可以做练习。我了解的基础知识,但我还是不能做正确的比特移位操作。

编辑这里是从K&安培的exersices; R已经难倒我

练习2-6:写功能setbits(X,P,N,Y),与n位thatbegin在位置p设定为y的最右边的n位返回x,留下其余位不变

练习2-7:写功能反转(X,P,N)与该开始以反转poisiton P上的n位返回x(即1变为0,反之亦然),留下其余不变

练习2-8:编写一个函数rightrot(X,N)返回整数的值X n个位的位置右移

练习2-9:在二的补数系统中,x&放大器; =(X-1)中删除x中的最右边的1位。解释为什么,使用此观察写位计数的更快版本

以上是从K&放演习; R(C语言),其预订的最佳Ç书是有的,但我无法理解bitshifting与这些练习,所以我有问题


解决方案

位移位也不过是它的的字面的意思是:所有移位位向左或向右一个给定的顺序。

所有您需要记住的是,每一个十进制数(如6,7,3,2)是psented为位在计算机的内存中的序列重新$ P $。所以,如果你发现这样的事情在一块C $ C $的c:

 (7 GT;&GT; 1)

这意味着在7底层的二进制重新presentation位都被右移1的位置移动。

我觉得你举的链接解释是pretty清楚。在纸自己说不定写下比特序列,并将它们操纵如在所引用的链接可以帮助

也许你不还可能有理解计算机如何与数字的内部工作。在这种情况下,学习有点移位前,你需要阅读这一点。

I have looked at http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting but I still find the concept of bit shifting difficult to understand.

Can someone point me in the direction of a more basic guide to bit shifting in C. I expect it will be something really long since it will need to cover the whole subject.

I really do not understand it but I want to learn it, so any help would be appreciated.

I am learning the k&r, and that is what this is for so I can do the exercises. I understand the basics, but I still cannot do correct bit shift operations.

EDIT here's the exersices from K&R that have stumped me

Exercise 2-6: Write a function setbits(x, p, n, y) that returns x with the n bits thatbegin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

Exercise 2-7: Write a function invert(x, p, n) that returns x with the n bits that begin at poisiton p inverted (i.e., 1 changed into 0 and vice versa) leaving the others unchanged.

Exercise 2-8: Write a function rightrot(x, n) that returns the value of the integer x rotated to the right by n bit positions

Exercise 2-9: In a two's complement number system, x &= (x-1) deletes the rightmost 1-bit in x. Explain why, Use this observation to write a faster version of bitcount

These are exercises from the k&R (the c programming language) book its the best c book there is but I am having trouble understanding bitshifting so I am having problems with these exercises.

解决方案

Bit shifting is nothing but what it literally means: shifting all bits in a given sequence left or right.

All you have to remember is that every decimal number (like 6, 7, 3, 2) is represented as a sequence of bits in the memory of the computer. So if you find something like this in a piece of C code:

(7 >> 1)

it means that the bits in the underlying binary representation of 7 are to be shifted right by 1 position.

I think the explanation in the link you cite is pretty clear. Maybe writing down a sequence of bits on a paper yourself and manipulating them as in the cited link can help.

Or perhaps you don't possibly yet have the understanding how computers work with numbers internally. In that case, before learning bit shifting, you need to read about that.

这篇关于我在哪里可以找到对C有点移位引导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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