Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check if a number is a multiple of 9 using bitwise operators in C++

1 Answer

0 votes
#include <iostream>

bool checkmMultipleOf9UsingBitwise(int n) {
    if (n == 0 || n == 9) {
        return true;
    }
    else if (n < 9) {
            return false;
    }
        else {
            /* 
                until n becomes either:
                    0 or 9 → return true (it's a multiple of 9)
                    < 9 but not equal to 0 or 9 → return false
            */
            return checkmMultipleOf9UsingBitwise((int)(n >> 3) - (int)(n & 7));
        }
}
 
int main()
{
    int num = 72; // 8 * 9
    
    if (checkmMultipleOf9UsingBitwise(num)) {
        std::cout << "The Number is multiple of 9 \n" << std::endl;
    }
    else {
        std::cout << "The Number is not multiple of 9 \n" << std::endl;
    }
}



/*
run:

The Number is multiple of 9 

*/

 



answered Oct 25, 2025 by avibootz
edited Oct 25, 2025 by avibootz
...