📌  相关文章
📜  使用按位运算运算符检查数字是否为9的倍数

📅  最后修改于: 2021-04-27 21:46:41             🧑  作者: Mango

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

C++
// C++ program to check if a number
// is multiple of 9 using bitwise operators
#include 
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
> 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


输出:

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

这是如何运作的?
n / 9可以使用以下简单公式以n / 8的形式编写。

n/9 = n/8 - n/72

由于我们需要使用按位运算运算符,因此我们使用n >> 3获得floor(n / 8)的值,并使用n&7获得n%8的值。我们需要用floor(n / 8)n%8来写上面的表达式。
n / 8等于“ floor(n / 8)+(n%8)/ 8” 。让我们用floor(n / 8)n%8来写上面的表达式

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