bitwise_and使用_C ++程序使用Bitwise运算符检查数字是否为2的幂

bitwise_and使用

Given a number and we have to check whether it is power of 2 or not.

给定一个数字,我们必须检查它是否为2的幂。

An obvious approach is to divide the number by 2 until the number will not get 0 or 1. But This Algorithm has the problem of Time complexity. This algorithm takes the O(log(n)) complexity to solve such a simple problem.

一种明显的方法是将数字除以2,直到数字不为0或1。但是此算法存在时间复杂性的问题。 该算法采用O(log(n))复杂度来解决这样一个简单的问题。

So how can we improve this?, Can this be solved in order of O(1) complexity?

那么我们该如何改善呢?能否按O(1)复杂度的顺序解决?

The answer to this is "yes", with the help of the bitwise operator.

在按位运算符的帮助下,答案是“是”

But, before this, we have to know about a simple property of binary number which is "the power of 2 having only one set bit in their Binary representation".

但是,在此之前,我们必须知道一个二进制数的简单属性,即“ 2的幂在其二进制表示中只有一个置位”

For example,

例如,

     2 =    0001
     4 =    0100
     8 =    1000
    16 =   10000
    32 =  100000
    64 = 1000000
    so on..

Now, let be more precise about this,

现在,让我们对此更加精确,

If we subtract 1 from the power of 2 what we get is 1s at all the places from the end of the binary number till we will not get 1 set bit. And, if we apply Bitwise AND operator we should only get zeros.

如果我们从2的幂中减去1,那么从二进制数的末尾到所有位都将得到1s,直到得到1个置位为止。 而且,如果我们应用按位AND运算符,我们应该只能得到零。

This will get clearer with this example:

通过以下示例,这将变得更加清晰:

    Let the no n =16

    Binary representation of 16 is 
        16  =  0 0 0 1 0 0 0 0
        16-1=  0 0 0 0 1 1 1 1
        ----------------------------
    Now 16 AND 15=  0 0 0 0 0 0 0 0

    Hence , 16 is the power of 2

    But what in case if it is not:

    Let the no n=15
        15 =  0 0 0 0 1 1 1 1    
        14 =  0 0 0 0 1 1 1 0
        ----------------------------
    15 AND 14=  0 0 0 0 1 1 1 1

    So 15 is not the power of 2.

Now, this can be implemented with order of complexity 1

现在,这可以按照复杂度的顺序实现1

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

Example:

例:

    INPUT:
    n = 4
    n-1 = 3 
    n & n-1 = 0
    (AND of 100 with 011) 
    Output= TRUE

    INPUT:
    n = 9
    n-1 = 8
    n & n-1 = 9 
    (AND of 1001 with 1000)
    Output= FALSE

    INPUT:
    n = 15
    n-1 = 14 
    n & n-1 = 14
    (AND of 01111 with 01110)
    Output=FALSE

    INPUT:
    n = 16
    n-1 = 15 
    n & n-1 = 0 
    (AND of 10000 with 01111)
    Output= TRUE

C++ implementation

C ++实现

// program to find that the number is power of 2
#include<iostream>
using namespace std;

int main()
{
	// declaring the array n
	int n[]={4,9,15,16,20,22,25,32,45,64,72,80,100,128,256};
	int i;
	
	for(i=0;i<15;i++)
	{
		cout<< n[i] << " is power of 2 : ";
		// use of bitwise AND (&) operator
		if((n[i]& (n[i]-1))==0) 
			cout<<"True"<<endl;
		else
			cout<<"False"<<endl;
	}
	
	return 0;	
}

Output

输出量

4 is power of 2 : True 
9 is power of 2 : False
15 is power of 2 : False 
16 is power of 2 : True
20 is power of 2 : False 
22 is power of 2 : False 
25 is power of 2 : False 
32 is power of 2 : True
45 is power of 2 : False 
64 is power of 2 : True
72 is power of 2 : False 
80 is power of 2 : False 
100 is power of 2 : False
128 is power of 2 : True 
256 is power of 2 : True 


翻译自: https://www.includehelp.com/cpp-programs/check-if-number-is-power-of-2-using-bitwise-operator.aspx

bitwise_and使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值