当前位置:网站首页>Leetcode-190. inverting binary bits
Leetcode-190. inverting binary bits
2022-07-28 13:23:00 【KGundam】
An operation
Topic details
Invert given 32 The binary bit of an unsigned integer .
Tips :
Please note that , In some languages ( Such as Java) in , There is no unsigned integer type . under these circumstances , Both input and output will be specified as signed integer types , And should not affect your implementation , Because whether integers are signed or unsigned , Its internal binary representation is the same .
stay Java in , The compiler uses binary complement notation to represent signed integers . therefore , stay Example 2 in , The input represents a signed integer -3, The output represents a signed integer -1073741825.
Example 1:
Input :n = 00000010100101000001111010011100
Output :964176192 (00111001011110000010100101000000)
explain : Binary string of input 00000010100101000001111010011100 Represents an unsigned integer 43261596,
Therefore return 964176192, Its binary representation is 00111001011110000010100101000000.
Example 2:
Input :n = 11111111111111111111111111111101
Output :3221225471 (10111111111111111111111111111111)
explain : Binary string of input 11111111111111111111111111111101 Represents an unsigned integer 4294967293,
Therefore return 3221225471 Its binary representation is 10111111111111111111111111111111 .
Ideas :
Use arithmetic shift left and right , You can flip , Every time res Move left and fill on the right 0, And then take n The last one is filled in res
Then make n Move right , In order to remove one , for instance :
My code :
class Solution
{
public:
uint32_t reverseBits(uint32_t n)
{
uint32_t res = 0;
for (int i = 0; i < 32; ++i)
{
res <<= 1; // Answer shift left
res += n & 1; // take n last place
n >>= 1; //n Move right
}
return res;
}
};
You can also use divide and conquer , Divide the binary string into two parts , Exchange the left and right parts , Then use the same method to deal with the left and right parts respectively until all parts are exchanged , That is, the flip is completed
class Solution
{
public:
uint32_t reverseBits(uint32_t n)
{
n = (n >> 16) | (n << 16);
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4);
n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2);
n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1);
return n;
}
};
Common skills of bit operation

边栏推荐
- leetcode-190.颠倒二进制位
- Why is crypto game changing the game industry?
- 10、 Kubernetes scheduling principle
- Getderivedstatefromprops lifecycle
- 机器学习基础-决策树-12
- .net for subtraction, intersection and union of complex type sets
- Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho
- How to design a second kill system?
- Storage model: big end and small end
- [June 28 event preview] low code Summit
猜你喜欢

Low code: reduce technical capability requirements and improve software development efficiency

Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
![[embedded C foundation] Part 6: super detailed explanation of common input and output functions](/img/eb/69264bc0d8e9349991b7b9e1b8ca22.png)
[embedded C foundation] Part 6: super detailed explanation of common input and output functions

Phpstudy steps to quickly build a website (teach you to build it by hand)
![[July 5 event preview] Flink Summit](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[July 5 event preview] Flink Summit

LeetCode·每日一题·1331.数组序号转换·离散化

Chapter 6 promotion

How to use databricks for data analysis on tidb cloud | tidb cloud User Guide

Xampp Chinese tutorial guide

如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
随机推荐
gicv3 spi register
SSM框架网上书城全套
黑猫带你学eMMC协议第27篇:什么是eMMC的动态容量(Dynamic Capacity)?
UV germicidal lamp chip dlt8p65sa Jericho
【嵌入式C基础】第6篇:超详细的常用的输入输出函数讲解
Li FuPan: application practice of kata safety container in ant group
Jetpack Compose 完全脱离 View 系统了吗?
How much do you know about JVM memory management
[embedded C foundation] Part 5: original code / inverse code / complement code
[FPGA] joint simulation of vivado and Modelsim
nport串口服务器配置网址(串口服务器是不是网口转串口)
【C语言易错点】第4篇:结构体在内存中存储规则详讲
Xampp Chinese tutorial guide
Code layered management of interface testing based on RF framework
Have a part of the game, after NFT is disabled in my world
Databinding+LiveData轻松实现无重启换肤
BiliBili Yang Zhou: above efficiency, efficient delivery
Leetcode 笔记 566. 重塑矩阵
Change the document type in endnode and import it in word
Black cat takes you to learn EMMC protocol chapter 27: what is EMMC's dynamic capacity?