当前位置:网站首页>Leetcdoe-342. Power of 4
Leetcdoe-342. Power of 4
2022-07-28 13:23:00 【KGundam】
An operation
Topic details
Given an integer , Write a function to determine if it is 4 Power square . If it is , return true ; otherwise , return false .
Integers n yes 4 To the power of : There are integers x bring n == 4^x
Example 1:
Input :n = 16
Output :true
Example 2:
Input :n = 5
Output :false
Example 3:
Input :n = 1
Output :true
Ideas :
When a number n yes 2 Integer power of : Then its binary must be 0...010...0 Form like this ( Only one is 1)
n-1 The binary of is 0...001...1 You can get : n & (n-1) == 0 ==> This number is 2 Integer power of
So if n It's also 4 The integer power of ? So the binary 1 The position of must be odd ( And only this one ), for example 0...010...1000...10000 Title n The range is -2^31 <= n <= 2^31 - 1 Binary 32 position
So we can use 10101...101(32 The odd digits are all 1 Even digits are all 0) Give Way n Do and calculate with it
If the result is not 0 explain n There is one odd digit in the binary of 1, namely n yes 4 Integer power of
My code :
class Solution
{
public:
bool isPowerOfFour(int n)
{
// n by 2 Integer power of 10101...101 The hexadecimal representation of
return n > 0 && !(n & (n - 1)) && (n & 0x55555555);
}
};
Common skills of bit operation

边栏推荐
- 我抄底了被清算的NFT,却被OpenSea上了锁
- 黑猫带你学eMMC协议第24篇:eMMC的总线测试程序详解(CMD19 & CMD14)
- Analysis of Andriod low on memory printing principle
- Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
- With 433 remote control UV lamp touch chip-dlt8sa20a-jericho
- [embedded C foundation] Part 5: original code / inverse code / complement code
- Black Scholes Merton European option pricing formula
- Call / put option price curve
- Le transaction
- Chapter 6 promotion
猜你喜欢

Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho

【嵌入式C基础】第1篇:基本数据类型
![[matlab]: FFT related problems](/img/08/c721394c172bdfd1d5d55aef31ccbf.png)
[matlab]: FFT related problems

How much do you know about JVM memory management

黑猫带你学eMMC协议第24篇:eMMC的总线测试程序详解(CMD19 & CMD14)
![[matlab] IIR filter](/img/60/8e666bff3d458cdd9367ca45112b92.png)
[matlab] IIR filter

Risk analysis of option trading

MySQL 实践篇 —— 主从复制

为什么说Crypto游戏正在改变游戏产业?

Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
随机推荐
One track education, PHP training, unity of knowledge and practice, popular
How to improve deep learning performance?
Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
I copied the bottom of the liquidated NFT, but was locked by opensea
butterfly spreads
Aragon创建DAO polygon BSC测试网
Dry goods -- encapsulated anti shake and throttling method in the project
Chapter 6 提升
Bear market spread portfolio
Databinding+LiveData轻松实现无重启换肤
The essence of enterprise Digitalization
Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
Black Scholes Merton European option pricing formula
ES6 null merge operator (?)
Comments are not allowed in JSON
Full disclosure! Huawei cloud distributed cloud native technology and Practice
kotlin学习笔记3——Lambda编程
Complete set of SSM framework online bookstore
Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho
Interview must ask, focus! Tell me about the Android application startup process and its source code?