使用位运算符检查数字是否为9的倍数

给定一个数字n,编写一个函数,如果n可被9整除,则返回true,否则返回false。检查n被9整除的最简单方法是执行n%9。 另一种方法是对n的数字求和。如果数字之和是9的倍数,那么n是9的倍数。 上述方法不是基于位运算符的方法,需要使用%和/。 这个 定义的位运算 通常比模和除法运算符更快。下面是一个基于位运算符的方法来检查9的整除性。

null

C++

// C++ program to check if a number
// is multiple of 9 using bitwise operators
#include <bits/stdc++.h>
using namespace std;
// Bitwise operator based function to check divisibility by 9
bool isDivBy9( int n)
{
// Base cases
if (n == 0 || n == 9)
return true ;
if (n < 9)
return false ;
// If n is greater than 9, then recur for [floor(n/9) - n%8]
return isDivBy9(( int )(n >> 3) - ( int )(n & 7));
}
// Driver program to test above function
int main()
{
// Let us print all multiples of 9 from 0 to 100
// using above method
for ( int i = 0; i < 100; i++)
if (isDivBy9(i))
cout << i << " " ;
return 0;
}


JAVA

// Java program to check if a number
// is multiple of 9 using bitwise operators
import java.lang.*;
class GFG {
// Bitwise operator based function
// to check divisibility by 9
static boolean isDivBy9( int n)
{
// Base cases
if (n == 0 || n == 9 )
return true ;
if (n < 9 )
return false ;
// If n is greater than 9, then
// recur for [floor(n/9) - n%8]
return isDivBy9(( int )(n >> 3 ) - ( int )(n & 7 ));
}
// Driver code
public static void main(String arg[])
{
// Let us print all multiples of 9 from
// 0 to 100 using above method
for ( int i = 0 ; i < 100 ; i++)
if (isDivBy9(i))
System.out.print(i + " " );
}
}
// This code is contributed by Anant Agarwal.


Python3

# Bitwise operator based
# function to check divisibility by 9
def isDivBy9(n):
# Base cases
if (n = = 0 or n = = 9 ):
return True
if (n < 9 ):
return False
# If n is greater than 9,
# then recur for [floor(n / 9) - n % 8]
return isDivBy9(( int )(n>> 3 ) - ( int )(n& 7 ))
# Driver code
# Let us print all multiples
# of 9 from 0 to 100
# using above method
for i in range ( 100 ):
if (isDivBy9(i)):
print (i, " " , end = "")
# This code is contributed
# by Anant Agarwal.


C#

// C# program to check if a number
// is multiple of 9 using bitwise operators
using System;
class GFG {
// Bitwise operator based function
// to check divisibility by 9
static bool isDivBy9( int n)
{
// Base cases
if (n == 0 || n == 9)
return true ;
if (n < 9)
return false ;
// If n is greater than 9, then
// recur for [floor(n/9) - n%8]
return isDivBy9(( int )(n >> 3) - ( int )(n & 7));
}
// Driver code
public static void Main()
{
// Let us print all multiples of 9 from
// 0 to 100 using above method
for ( int i = 0; i < 100; i++)
if (isDivBy9(i))
Console.Write(i + " " );
}
}
// This code is contributed by nitin mittal.


PHP

<?php
// PHP program to check if a number
// is multiple of 9 using bitwise
// operators
// Bitwise operator based function
// to check divisibility by 9
function isDivBy9( $n )
{
// Base cases
if ( $n == 0 || $n == 9)
return true;
if ( $n < 9)
return false;
// If n is greater than 9,
// then recur for [floor(n/9) -
// n%8]
return isDivBy9(( $n >> 3) -
( $n & 7));
}
// Driver Code
// Let us print all multiples
// of 9 from 0 to 100
// using above method
for ( $i = 0; $i < 100; $i ++)
if (isDivBy9( $i ))
echo $i , " " ;
// This code is contributed by nitin mittal
?>


Javascript

<script>
// javascript program to check if a number
// is multiple of 9 using bitwise operators
// Bitwise operator based function
// to check divisibility by 9
function isDivBy9(n)
{
// Base cases
if (n == 0 || n == 9)
return true ;
if (n < 9)
return false ;
// If n is greater than 9, then
// recur for [floor(n/9) - n%8]
return isDivBy9(parseInt(n >> 3) - parseInt(n & 7));
}
// Driver code
// Let us print all multiples of 9 from
// 0 to 100 using above method
for (i = 0; i < 100; i++)
if (isDivBy9(i))
document.write(i + " " );
// This code is contributed by Princi Singh
</script>


输出:

0 9 18 27 36 45 54 63 72 81 90 99

这是怎么回事? n/9 可以写成 n/8 使用下面的简单公式。

n/9 = n/8 - n/72

因为我们需要使用位运算符,所以我们得到 楼层(n/8) 使用 n> >3 并从中获得价值 n%8 使用 n&7 .我们需要写上面的表达方式 楼层(n/8) n%8 . n/8 等于 “楼层(n/8)+(n%8)/8” .让我们把上面的表达式写成 楼层(n/8) n%8

n/9 = floor(n/8) + (n%8)/8 - [floor(n/8) + (n%8)/8]/9n/9 = floor(n/8) - [floor(n/8) - 9(n%8)/8 + (n%8)/8]/9n/9 = floor(n/8) - [floor(n/8) - n%8]/9

从上面的等式中, N 是的倍数 9 只有当表达 楼层(n/8)–[楼层(n/8)–n%8]/9 是一个整数。如果子表达式 [楼层(n/8)–n%8]/9 是一个整数。子表达式只能是整数,如果 [楼层(n/8)–n%8] 是的倍数 9 因此,问题简化为一个较小的值,可以用位运算符来写。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

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