博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rotate bits of a number
阅读量:4151 次
发布时间:2019-05-25

本文共 839 字,大约阅读时间需要 2 分钟。

reference: 

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<
>(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));}

转载地址:http://texti.baihongyu.com/

你可能感兴趣的文章
一道技术问题引起的遐想,最后得出结论技术的本质是多么的朴实!
查看>>
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>