当前位置:网站首页>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

边栏推荐
- Resolve browser password echo
- MySQL practice -- master-slave replication
- One track education, PHP training, unity of knowledge and practice, popular
- ES6 null merge operator (?)
- 【嵌入式C基础】第3篇:常量和变量
- Is jetpack compose completely out of view?
- [embedded C foundation] Part 5: original code / inverse code / complement code
- Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
- Black Scholes Merton European option pricing formula
- [FPGA]: MATLAB generates COE files
猜你喜欢

LeetCode每日一题(2196. Create Binary Tree From Descriptions)

UV germicidal lamp chip dlt8p65sa Jericho

Understanding of vite2

黑猫带你学eMMC协议第26篇:eMMC的硬件复位操作(H/W reset)
![[error prone points of C language] Part 4: detailed rules for storing structures in memory](/img/87/555e6db40f23b5bd4919bd7bdce776.png)
[error prone points of C language] Part 4: detailed rules for storing structures in memory

Shell基础概念和变量

Bear market spread portfolio

如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南

Intrinsic value and time value of options

Protective bearish strategy
随机推荐
Databinding+LiveData轻松实现无重启换肤
Vditor 渲染器如何做到服务端渲染(SSR)?
[embedded C foundation] Part 9: basic usage of C language pointer
Have a part of the game, after NFT is disabled in my world
为什么说Crypto游戏正在改变游戏产业?
LeetCode·每日一题·1331.数组序号转换·离散化
什么是事务及数据库的优化方法
Black cat takes you to learn UFS agreement part 2: Interpretation of UFS related terms
Chapter 6 提升
Complete set of SSM framework online bookstore
Storage model: big end and small end
gicv3 spi register
Using auto.js to realize the function of fifaol3 mobile terminal card interceptor
Intrinsic value and time value of options
管理区解耦架构见过吗?能帮客户搞定大难题的
Why is crypto game changing the game industry?
How to improve deep learning performance?
One track education, PHP training, unity of knowledge and practice, popular
Databinding+livedata can easily realize skin changing without restart
Analysis of Andriod low on memory printing principle