Check if a number is divisible by 8 using bitwise operators
Given a number n, check if it is divisible by 8 using bitwise operators.
Examples:
Input : 16 Output :YES Input :15 Output :NO
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 = 16if (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. ?> |
YES
Recommended Posts:
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is multiple of 9 using bitwise operators
- Check if n is divisible by power of 2 without using arithmetic operators
- Check if a number is multiple of 5 without using / and % operators
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Check if a number is positive, negative or zero using bit operators
- Bitwise Operators in C/C++
- What are the differences between bitwise and logical AND operators in C/C++?
- Bitwise right shift operators in Java
- Toggle case of a string using Bitwise Operators
- Check if a number is divisible by 23 or not
- Check if a number is divisible by 41 or not
- Check if a number is divisible by all prime divisors of another number
- Russian Peasant (Multiply two numbers using bitwise operators)
- Check if a large number is divisible by 5 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



