leetcode——190 Reverse Bits(32位无符号二进制数的翻转)

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up:

If this function is called many times, how would you optimize it?

Hide Tags: Bit Manipulation(位操作)

解题思路:

(1)将需要翻转的数n跟1进行“&”运算,取得最低位上的数,

(2)左移到对应位置上,实现翻转。

(3)将翻转的结果加入到result中

(4)将n>>1右移一位,继续遍历

代码如下:

	public static int reverseBits(int n)
	{
		int result=0;
		for (int i = 0; i < 32; i++)
		{
			//获取n对应的二进制数的最右边(最低位)上的数
			int temp=n&1;
			//左移到对应位置上,实现翻转
			int reverseTemp=temp<<(31-i);
			//将翻转的结果加入到result中
			result=result|reverseTemp;
			//进入次低位 >>>:表示无符号数的右移动
			n=n>>>1;
		}
		return result;
	}
时间: 2024-08-02 02:37:58

leetcode——190 Reverse Bits(32位无符号二进制数的翻转)的相关文章

[LeetCode] 190. Reverse Bits 翻转二进制位

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up:If this function

LeetCode 190. Reverse Bits (反转位)

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 题目标签: Bit Manipulation 这道题目

Leetcode 190. Reverse Bits(反转比特数)

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up: If this function

Java for LeetCode 190 Reverse Bits

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). JAVA实现如下: public int revers

汇编程序 - 1 (32位无符号乘法)

这个是学校的课程设计<微机原理与接口技术>内容,自己写得有些缺陷,但基本实现了运算功能,暂且记录,方便日后回顾,也供大家参考. 缺陷: 1. 只能固定长度输入(32位对应为00000000 -- FFFFFFFF) 2. 例如输入6(16进制)只能类似输入00000006 3.只考虑了0-F中的0-9输入,也没考虑A-F或者a-f 暂时没空修改,以后有空或许会再写写 实验环境: 1.我是在虚拟机上的Windows 7 调试的,TD调试中显示的cpu是80486,但本次实验要求所用8086(16

Leetcode 190 Reverse Bits 位运算

反转二进制 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 uint32_t ans = 0; 5 for (int i = 0; i<32; ++i,n >>=1){ 6 ans = (ans<< 1)|(n & 1); 7 } 8 return ans; 9 } 10 };

Java [Leetcode 190]Reverse Bits

题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 解题思路: 移位操作. 代码如下: publi

C#Winform基础 十进制整数转换为32位有符号二进制数

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 UI 2 code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using S

leetcode:Reverse Bits

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). Follow up:If this function i