Rotate bits of a number
来源:互联网 发布:java视频教程免费下载 编辑:程序博客网 时间:2024/05/24 02:16
reference:
http://www.geeksforgeeks.org/rotate-bits-of-an-integer/
Problem Definition:
Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.
In left rotation, the bits that fall off at left end are put back at right end.
In right rotation, the bits that fall off at right end are put back at left end.
Solution:
Do the navie shift first, and then add the missing information to it again.
Code:
/*Function to left rotate n by d bits*/int leftRotate(int n, unsigned int d){ /* In n<<d, last d bits are 0. To put first 3 bits of n at last, do bitwise or of n<<d with n >>(INT_BITS - d) */ return (n << d)|(n >> (INT_BITS - d));} /*Function to right rotate n by d bits*/int rightRotate(int n, unsigned int d){ /* In n>>d, first d bits are 0. To put last 3 bits of at first, do bitwise or of n>>d with n <<(INT_BITS - d) */ return (n >> d)|(n << (INT_BITS - d));}
- Rotate bits of a number
- Number of 1 Bits && Rotate Array && Reverse Bits
- leetcode之Number of 1 Bits,Reverse Bits,Rotate Array
- Rotate bits of a number 左旋转或右旋转一个数 @GFG
- Number of Bits in a Decimal Integer
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- Number of 1 Bits
- 消息软删除
- Bison-Flex 笔记
- Java学习笔记45:Java 线程与主线程之间的变量关系
- 关于Ovirt的TUI库
- uva 264Count on Cantor
- Rotate bits of a number
- extjs 和 jquery区别
- CE with requiem
- wdcp常用工具及命令集
- 网页制作之Css带来的好处
- 教你如何清肠,教你早晨清肠、除口臭、色斑、大肚腩
- iPad2 iOS 5.0.1+ Xcode 4.3 免证书(iDP)开发 + 真机调试 生成IPA全攻略
- 10 Years
- vim颜色自定义