231. Power of Two
来源:互联网 发布:网络组件设备方案 编辑:程序博客网 时间:2024/05/17 02:09
题目
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question.
思路
判断一个数是否是2的指数,本质上就是判断此数是否是正数,且bit只有一个为1
代码
class Solution {public: bool isPowerOfTwo(int n) { //判断入参是不是2的指数,就是二进制bit只有一个1 //题目转化为判断入参的bit是否是正数且只有一个1, if(n <= 0) { return false; } return !(n&(n-1)); }};
0 0
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- Android 动画
- leetcode---Missing Number---位
- Linux驱动基础:MSM平台AP/CP通信机制
- linux free命令
- 单精度浮点(32bit float)与半精度浮点(16bit)的理解及开根号求倒数算法
- 231. Power of Two
- 今客CRM客户关系授权码 今客CRM客户关系破解版
- 深入理解HTTP协议、HTTP协议原理分析
- Linux ALSA声卡驱动
- centos 7 x86_64上安装staruml2.8.0
- BARCODE条形码插件及ean13
- oracle一
- WEB-request&response
- Xshell在按delete,backspace键产生乱码的解决方法