check a int number is power of 2?

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:how many does the factorial of n have zero?

博客时间:2014-5-7;

编程语言:Java ;

编程坏境:Windows 7 专业版 x64;

编程工具:jdk,eclipse x64;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;

my words

every problem has a or more solutions.

problem

eg: 1 = 2^0;

2 = 2^1;

4 = 2^2;

5 != 2^2 and 5 != 2^3

make true the function: bool  _check_power_2(int n)

program run out follows

0 false
1 true
2 true
3 false
4 true
5 false
6 false
7 false
8 true
9 false
10 false
11 false
12 false
13 false
14 false
15 false
16 true
17 false
18 false
19 false
20 false
21 false
22 false
23 false
24 false
25 false
26 false
27 false
28 false
29 false
30 false
31 false
32 true
33 false
34 false
35 false
36 false
37 false
38 false
39 false
40 false
41 false
42 false
43 false
44 false
45 false
46 false
47 false
48 false
49 false
50 false
51 false
52 false
53 false
54 false
55 false
56 false
57 false
58 false
59 false
60 false
61 false
62 false
63 false
64 true
65 false
66 false
67 false
68 false
69 false
70 false
71 false
72 false
73 false
74 false
75 false
76 false
77 false
78 false
79 false
80 false
81 false
82 false
83 false
84 false
85 false
86 false
87 false
88 false
89 false
90 false
91 false
92 false
93 false
94 false
95 false
96 false
97 false
98 false
99 false

my solution

as for a int number n

the binary data of n only have one 1, then it is, or not.

eg:

find regularity, the number of power of 2 only has one 1.

so if( n & (n-1) == 0 ) return true; else return false;

my code

package test;

public class test {

	public static void main(String[] args) {
		for(int i=0;i<100;i++)
		{
			System.out.println(i+" "+_2_pow(i));
		}

	}
	static boolean _2_pow(int n)
	{
		if( n > 0 )
		{
			int i = (n-1) & n ;
			if(i == 0)
				return true;
			else return false;
		}
		else return false ;
	}

}

check a int number is power of 2?,布布扣,bubuko.com

时间: 2024-08-10 14:41:39

check a int number is power of 2?的相关文章

判断一个整数是否为回文数 Check if a number is palindrome

一种方法是先翻转当前数,然后把它和原数比较(略) 另一种是递归方法,借用一个复制数,对原数递归,使之翻转,然后配合复制数比较 package recursion; public class Check_if_a_number_is_palindrome { public static void main(String[] args) { int num = 121321; System.out.println(check(num)); num = 12321; System.out.printl

Js String转Int(Number与parseInt的区别)

<script>     var   str='1250' ;  alert( Number(str) );  //得到1250 alert(parseInt(str));  //得到1250 var str1='00100'; alert( Number(str1) );  //得到100 alert(parseInt(str1));  //得到64 发现parseInt方法在format'00'开头的数字时会当作2进制转10进制的方法进行转换,所以建议string转int最好用Number

LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array

原题链接在这里:https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/ 题目: Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element. A majority element is

LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: 1 class Solution { 2 public: 3 bool isP

Check if a large number is divisible by 3 or not

1 //检验一个大数是否能3整除 2 //A number is divisible by 3 if sum of its digits is divisible by 3. 3 //we cannot use n % 3 to check if a number is divisible by 3 or not. 4 //Remainder of 10i divided by 3 is 1 So powers of 10 only result in value 1. 5 #include<b

LeetCode 342. Power of Four (4的次方)

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目标签:Bit Manipulation 这道题目让我们判断一个数字是不是4的

Round Up To Power Of Two

这个标题应该说明了我们要做什么了,中文的意思是找出一个2^n的数,使其不小于给出的数字.举个例子吧: 如果给一个数字63,那么我需要获取不小于63的数字,但是这个数字需要是2的n次方了,所以 63对应的是64(2^6) 64对应的依旧是64(2^6) 100对应的是128(2^7) 问题来了: 怎么快速的计算出这个结果呢? 可能首先浮现在我们眼前的可能是计算log或者一些其他的一些非位操作的算法,这些算法就不再次说明,来看一下JDK以及android的源码包中是怎么来计算的. HashMap是一

[LeetCode] Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 判断一个数是否是4的幂,如果一个数是4的幂,则这个数的二进制的1在偶数位上.所以

342. Power of Four【位运算】

2017/3/23 22:23:57 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 思路:题目要求不能循环或递归,这里采用位运