Write an Efficient C Program to Reverse Bits of a Number
来源:互联网 发布:文明5 for mac 汉化 编辑:程序博客网 时间:2024/05/16 10:54
reference:
http://www.geeksforgeeks.org/write-an-efficient-c-program-to-reverse-bits-of-a-number/
Problem Definition:
Write an Efficient C Program to Reverse Bits of a Number
Solution:
Loop through all the bits of an integer. If a bit at ith position is set in the i/p no. then set the bit at (NO_OF_BITS – 1) – i in o/p. Where NO_OF_BITS is number of bits present in the given number.
Code:
unsigned int reverseBits(unsigned int num){ unsigned int NO_OF_BITS = sizeof(num) * 8; unsigned int reverse_num = 0; int i; for (i = 0; i < NO_OF_BITS; i++) { if((num & (1 << i))) reverse_num |= 1 << ((NO_OF_BITS - 1) - i); } return reverse_num;}
- Write an Efficient C Program to Reverse Bits of a Number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
- Write a C program to find the parity of an unsigned integer
- Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
- 【NO.5】c program to find factorial of a number.
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- Write a C program to print all permutations of a given string
- Six Steps to write an ILE C CGI program
- Reverse bits of an unsigned integer
- Rotate bits of a number
- Write a program to convert string to number without using library function。
- [CrackCode] 1.2 Write code to reverse a C-Style String
- 1.2 Write code to reverse a C-Style String.
- Number of 1 Bits && Rotate Array && Reverse Bits
- leetcode之Number of 1 Bits,Reverse Bits,Rotate Array
- leetcode-190&191 Reverse Bits & Number of 1 Bits
- Bit Manipulation-----190. Reverse Bits&&191. Number of 1 Bits
- Android Rss阅读器
- 【xinfanqie】新手教程:如何让Win 7更新完后不自动重启
- magento添加客户自定义字段
- C#基础概念二十五问
- 浅谈ClassLoader
- Write an Efficient C Program to Reverse Bits of a Number
- Android ViewPager使用详解
- Extjs Tab closeTab 显示中文
- Loadrunner的使用
- textView 添加超链接
- c++ 如何获取系统时间??
- 【文章教程】win 2003系统设置有哪些诀窍?
- 回调接口
- mac杂项