Check if a number is divisible by 8 using bitwise operators - GeeksforGeeks

05 Apr, 2021
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
Java
Python3
C#
PHP
Javascript
// 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// operatorint Div_by_8(int n){    return (((n >> 3) << 3) == n);} // Driver programint main(){    int n = 16;    if (Div_by_8(n))        cout << "YES" << endl;    else        cout << "NO" << endl;    return 0;}
// Java program to check whether// the number is divisible by// 8 or not using bitwise operatorimport 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
# 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 coden = 16if (Div_by_8(n)):    print("YES")else:    print("NO") # This code is contributed by Gitanjali.
// 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 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// operatorfunction 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.?>
<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// operatorfunction 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
AnirudhTiwari
Mithun Kumar
princi singh
divisibility
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Program to find whether a given number is power of 2
Add two numbers without using arithmetic operators
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7