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

边栏推荐
- 2021-10-06
- 2020jenkins study notes
- Android engineers, how to use kotlin to provide productivity?
- Auto.js enables Taobao to quickly submit orders
- How does the vditor renderer achieve server-side rendering (SSR)?
- Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
- Extended operator
- Full disclosure! Huawei cloud distributed cloud native technology and Practice
- 什么叫杂谈(e网杂谈)
- 【嵌入式C基础】第2篇:进制转换与BCD编码
猜你喜欢

Aragon创建DAO polygon BSC测试网
![[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088

Bear market spread portfolio

One track education, PHP training, unity of knowledge and practice, popular

FFT海浪模拟

MySQL 实践篇 —— 主从复制

How does kotlin help you avoid memory leaks?

黑猫带你学eMMC协议第26篇:eMMC的硬件复位操作(H/W reset)
![[July 5 event preview] Flink Summit](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[July 5 event preview] Flink Summit
![[embedded C foundation] Part 5: original code / inverse code / complement code](/img/85/16d5e9236e13870197d12386e3ab15.png)
[embedded C foundation] Part 5: original code / inverse code / complement code
随机推荐
kotlin学习笔记3——Lambda编程
Why is crypto game changing the game industry?
Why neural networks are ineffective?
Machine learning Basics - integrated learning-13
SSH port forwarding (Tunneling Technology)
黑猫带你学eMMC协议第27篇:什么是eMMC的动态容量(Dynamic Capacity)?
[FPGA]: Joint Simulation of FPGA and MATLAB
Leetcode 笔记 566. 重塑矩阵
Low code: reduce technical capability requirements and improve software development efficiency
[embedded C foundation] Part 5: original code / inverse code / complement code
【嵌入式C基础】第5篇:原码/反码/补码
GameStop熊市杀入NFT交易,老牌游戏零售商借Web3焕发第二春
面试必问,敲重点!讲一下 Android Application 启动流程及其源码?
[FPGA] joint simulation of vivado and Modelsim
Chapter 6 promotion
Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation
Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088
【嵌入式C基础】第1篇:基本数据类型
ES6 null merge operator (?)