Check if a number is divisible by 8 using bitwise operators Last Updated : 31 May, 2022 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a number n, check if it is divisible by 8 using bitwise operators. Examples: Input : 16 Output :YES Input :15 Output :NO Recommended PracticeCheck if a number is divisible by 8Try It! Approach: Result = (((n >> 3) << 3) == n). First we shift the 3 bit right then we shift the 3 bit left and then compare the number with the given number if the number is equal to the number then it is the divisible by 8 . Explanation: Example: n = 16 given so binary of the 16 is 10000 now we shift the 3 bit right, now we have 00010 so again we shift the 3 bit left, then we have 10000 now compare with the given number first 16==16 in binary so it true so the number is divisible by 8. CPP // C++ program to check whether // the number is divisible by // 8 or not using bitwise operator #include <bits/stdc++.h> using namespace std; // function to check number is // div by 8 or not using bitwise // operator int Div_by_8(int n) { return (((n >> 3) << 3) == n); } // Driver program int main() { int n = 16; if (Div_by_8(n)) cout << "YES" << endl; else cout << "NO" << endl; return 0; } Java // Java program to check whether // the number is divisible by // 8 or not using bitwise operator import java.io.*; import java.util.*; class GFG { // function to check number is // div by 8 or not using bitwise // operator static boolean Div_by_8(int n) { return (((n >> 3) << 3) == n); } // Driver code public static void main (String[] args) { int n = 16; if (Div_by_8(n)) System.out.println("YES"); else System.out.println("NO"); } } // This code is contributed by Gitanjali Python3 # Python program to check whether # the number is divisible by # 8 or not using bitwise operator import math # function to check number is # div by 8 or not using bitwise # operator def Div_by_8(n): return (((n >> 3) << 3) == n) # driver code n = 16 if (Div_by_8(n)): print("YES") else: print("NO") # This code is contributed by Gitanjali. C# // C# program to check whether // the number is divisible by // 8 or not using bitwise operator using System; class GFG { // function to check number is // div by 8 or not using bitwise // operator static bool Div_by_8(int n) { return (((n >> 3) << 3) == n); } // Driver code public static void Main () { int n = 16; if (Div_by_8(n)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed by vt_m. PHP <?php // PHP program to check whether // the number is divisible by // 8 or not using bitwise operator // function to check number is // div by 8 or not using bitwise // operator function Div_by_8($n) { return ((($n >> 3) << 3) == $n); } // Driver program $n = 16; if (Div_by_8($n)) echo "YES"; else echo "NO"; //This code is contributed by mits. ?> JavaScript <script> // javascript program to check whether // the number is divisible by // 8 or not using bitwise operator // function to check number is // div by 8 or not using bitwise // operator function Div_by_8(n) { return (((n >> 3) << 3) == n); } // Driver code var n = 16; if (Div_by_8(n)) document.write("YES"); else document.write("NO"); // This code is contributed by Princi Singh. </script> YES Time Complexity : O(1) Space Complexity : O(1) Comment More infoAdvertise with us Next Article Check if a number is divisible by 8 using bitwise operators A ajay0007 Follow Improve Article Tags : Bit Magic Mathematical Technical Scripter DSA divisibility +1 More Practice Tags : Bit MagicMathematical Similar Reads Check if a number is divisible by 17 using bitwise operators Given a number n, check if it is divisible by 17 using bitwise operators. Examples: Input : n = 34 Output : 34 is divisible by 17 Input : n = 43 Output : 43 is not divisible by 17 A naive approach will be to check it by % operator if it leaves a remainder of 0.To do division using Bitwise operators, 6 min read Check if a Number is Odd or Even using Bitwise Operators Given a number n, the task is to check whether the number is even or odd using Bitwise Operators.Examples: Input: n = 11 Output: OddInput: n = 10 Output: Even Following Bitwise Operators can be used to check if a number is odd or even:1. Using Bitwise XOR operator: The idea is to check whether the l 6 min read Check if a number is multiple of 9 using bitwise operators Given a number n, write a function that returns true if n is divisible by 9, else false. The most simple way to check for n's divisibility by 9 is to do n%9. Another method is to sum the digits of n. If sum of digits is multiple of 9, then n is multiple of 9. The above methods are not bitwise operat 5 min read Check if any permutation of a large number is divisible by 8 Given a large number N and the task is to check if any permutation of a large number is divisible by 8. Examples: Input: N = 31462708 Output: Yes Many of permutation of number N like 34678120, 34278160 are divisible by 8. Input: 75 Output: No A naive approach is to generate all permutations of the n 10 min read Check if n is divisible by power of 2 without using arithmetic operators Given two positive integers n and m. The problem is to check whether n is divisible by 2m or not without using arithmetic operators. Examples: Input : n = 8, m = 2 Output : Yes Input : n = 14, m = 3 Output : No Approach: If a number is divisible by 2 then it has its least significant bit (LSB) set t 4 min read Like