有效地检查n是否是4的倍数

给定一个数字 N .问题是要有效地检查 N 是4的倍数,或者不使用算术运算符。 例如:

null
Input : 16Output : YesInput : 14Output : No

方法: 4的倍数总是 00 作为二进制表示中的最后两位。我们必须检查 N 不管你是不是不安。 如何检查最后两位是否未设置。 如果n&3==0,则最后两位未设置,否则设置两位或其中一位。

C++

// C++ implementation to efficiently check whether n
// is a multiple of 4 or not
#include <bits/stdc++.h>
using namespace std;
// function to check whether 'n' is
// a multiple of 4 or not
string isAMultipleOf4( int n)
{
// if true, then 'n' is a multiple of 4
if ((n & 3) == 0)
return "Yes" ;
// else 'n' is not a multiple of 4
return "No" ;
}
// Driver program to test above
int main()
{
int n = 16;
cout << isAMultipleOf4(n);
return 0;
}


JAVA

// Java implementation to efficiently check
// whether n is a multiple of 4 or not
class GFG {
// method to check whether 'n' is
// a multiple of 4 or not
static boolean isAMultipleOf4( int n)
{
// if true, then 'n' is a multiple of 4
if ((n & 3 ) == 0 )
return true ;
// else 'n' is not a multiple of 4
return false ;
}
// Driver method
public static void main(String[] args)
{
int n = 16 ;
System.out.println(isAMultipleOf4(n) ? "Yes" : "No" );
}
}


Python 3

# Python 3 implementation to
# efficiently check whether n
# is a multiple of 4 or not
# function to check whether 'n'
# is a multiple of 4 or not
def isAMultipleOf4(n):
# if true, then 'n' is
# a multiple of 4
if ((n & 3 ) = = 0 ):
return "Yes"
# else 'n' is not a
# multiple of 4
return "No"
# Driver Code
if __name__ = = "__main__" :
n = 16
print (isAMultipleOf4(n))
# This code is contributed
# by ChitraNayal


C#

// C# implementation to efficiently check
// whether n is a multiple of 4 or not
using System;
class GFG {
// method to check whether 'n' is
// a multiple of 4 or not
static bool isAMultipleOf4( int n)
{
// if true, then 'n' is a multiple of 4
if ((n & 3) == 0)
return true ;
// else 'n' is not a multiple of 4
return false ;
}
// Driver method
public static void Main()
{
int n = 16;
Console.WriteLine(isAMultipleOf4(n) ? "Yes" : "No" );
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP implementation to
// efficiently check whether n
// is a multiple of 4 or not
// function to check whether 'n' is
// a multiple of 4 or not
function isAMultipleOf4( $n )
{
// if true, then 'n'
// is a multiple of 4
if (( $n & 3) == 0)
return "Yes" ;
// else 'n' is not
// a multiple of 4
return "No" ;
}
// Driver Code
$n = 16;
echo isAMultipleOf4( $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript implementation to efficiently check
// whether n is a multiple of 4 or not
// method to check whether 'n' is
// a multiple of 4 or not
function isAMultipleOf4(n)
{
// if true, then 'n' is a multiple of 4
if ((n & 3) == 0)
return true ;
// else 'n' is not a multiple of 4
return false ;
}
// Driver method
let n = 16;
document.write(isAMultipleOf4(n) ? "Yes" : "No" );
//This code is contributed by rag2127
</script>


输出:

Yes

我们能推广上述解决方案吗? 类似地,我们可以检查2的其他幂。例如,如果n&7为0,则数字n将是8的倍数。总的来说,我们可以说。

// x must be a power of 2 for below// logic to workif (n & (x -1) == 0)   n is a multiple of xElse   n is NOT a multiple of x

https://youtu.be/ke

-Mai2m6Cg 本文由 阿尤什·贾赫里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享