Skip to main content

Number divisible by 8 using bitwise

To check whether a number is divisible by 8 using bitwise operators, we can use the fact that any number that is a multiple of 8 will have its last three bits set to 0.

Here's how you can check if a number is divisible by 8 using bitwise operators in Python:

def is_divisible_by_8(num):
    return (num & 0b111) == 0

To check if a number is divisible by 8 using bitwise shift, we can use the following condition:

A number is divisible by 8 if and only if its three least significant bits are all 0.

We can check the three least significant bits of a number by performing a bitwise AND operation with the binary number 7 (which is 111 in binary with three bits).

To do this using bitwise shift, we can right shift the number by 3 bits, which is equivalent to dividing the number by 8. Then, we can left shift the result by 3 bits to get back the original number, and perform a bitwise AND operation with 7 to check if the three least significant bits are all 0.

Here's an example implementation in Python:

def is_divisible_by_8(n):
    return (n >> 3) << 3 == n and (n & 7) == 0

This function takes a number n as input, right shifts it by 3 bits to divide it by 8, left shifts the result by 3 bits to get back the original number, and performs a bitwise AND operation with 7 to check if the three least significant bits are all 0.

Note that this implementation assumes that n is a non-negative integer. If n can be a negative number, we need to use arithmetic shift instead of logical shift to preserve the sign of the number.

Here another example.

/*
    C program for
    Number divisible by 8 using bitwise
*/
#include <stdio.h>

void divisibleBy8(int num)
{
    // Shift right by 3
    int auxiliary = num >> 3;
    // Shift left by 3
    auxiliary = auxiliary << 3;
    // Check if auxiliary is equal to number or not
    if (auxiliary == num)
    {
        printf("\n Number %d divisible by 8", num);
    }
    else
    {
        printf("\n Number %d is not divisible by 8", num);
    }
}
int main(int argc, char const *argv[])
{
    // Test Inputs
    divisibleBy8(56);
    divisibleBy8(31);
    divisibleBy8(32);
    divisibleBy8(72);
    divisibleBy8(78);
    return 0;
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Java program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
    public void divisibleBy8(int num)
    {
        // Shift right by 3
        int auxiliary = num >> 3;
        // Shift left by 3
        auxiliary = auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            System.out.print("\n Number " + num + " divisible by 8");
        }
        else
        {
            System.out.print("\n Number " + num + " is not divisible by 8");
        }
    }
    public static void main(String[] args)
    {
        Divisibility task = new Divisibility();
        // Test Inputs
        task.divisibleBy8(56);
        task.divisibleBy8(31);
        task.divisibleBy8(32);
        task.divisibleBy8(72);
        task.divisibleBy8(78);
    }
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
    public: void divisibleBy8(int num)
    {
        // Shift right by 3
        int auxiliary = num >> 3;
        // Shift left by 3
        auxiliary = auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            cout << "\n Number " << num << " divisible by 8";
        }
        else
        {
            cout << "\n Number " << num << " is not divisible by 8";
        }
    }
};
int main()
{
    Divisibility *task = new Divisibility();
    // Test Inputs
    task->divisibleBy8(56);
    task->divisibleBy8(31);
    task->divisibleBy8(32);
    task->divisibleBy8(72);
    task->divisibleBy8(78);
    return 0;
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
// Include namespace system
using System;
/*
    Csharp program for
    Number divisible by 8 using bitwise
*/
public class Divisibility
{
    public void divisibleBy8(int num)
    {
        // Shift right by 3
        int auxiliary = num >> 3;
        // Shift left by 3
        auxiliary = auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            Console.Write("\n Number " + num + " divisible by 8");
        }
        else
        {
            Console.Write("\n Number " + num + " is not divisible by 8");
        }
    }
    public static void Main(String[] args)
    {
        Divisibility task = new Divisibility();
        // Test Inputs
        task.divisibleBy8(56);
        task.divisibleBy8(31);
        task.divisibleBy8(32);
        task.divisibleBy8(72);
        task.divisibleBy8(78);
    }
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
package main
import "fmt"
/*
    Go program for
    Number divisible by 8 using bitwise
*/

func divisibleBy8(num int) {
    // Shift right by 3
    var auxiliary int = num >> 3
    // Shift left by 3
    auxiliary = auxiliary << 3
    // Check if auxiliary is equal to number or not
    if auxiliary == num {
        fmt.Print("\n Number ", num, " divisible by 8")
    } else {
        fmt.Print("\n Number ", num, " is not divisible by 8")
    }
}
func main() {
    
    // Test Inputs
    divisibleBy8(56)
    divisibleBy8(31)
    divisibleBy8(32)
    divisibleBy8(72)
    divisibleBy8(78)
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
<?php
/*
    Php program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
    public  function divisibleBy8($num)
    {
        // Shift right by 3
        $auxiliary = $num >> 3;
        // Shift left by 3
        $auxiliary = $auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if ($auxiliary == $num)
        {
            echo("\n Number ".$num.
                " divisible by 8");
        }
        else
        {
            echo("\n Number ".$num.
                " is not divisible by 8");
        }
    }
}

function main()
{
    $task = new Divisibility();
    // Test Inputs
    $task->divisibleBy8(56);
    $task->divisibleBy8(31);
    $task->divisibleBy8(32);
    $task->divisibleBy8(72);
    $task->divisibleBy8(78);
}
main();

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Node JS program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
    divisibleBy8(num)
    {
        // Shift right by 3
        var auxiliary = num >> 3;
        // Shift left by 3
        auxiliary = auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            process.stdout.write("\n Number " + num + " divisible by 8");
        }
        else
        {
            process.stdout.write("\n Number " + num + " is not divisible by 8");
        }
    }
}

function main()
{
    var task = new Divisibility();
    // Test Inputs
    task.divisibleBy8(56);
    task.divisibleBy8(31);
    task.divisibleBy8(32);
    task.divisibleBy8(72);
    task.divisibleBy8(78);
}
main();

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
#    Python 3 program for
#    Number divisible by 8 using bitwise
class Divisibility :
    def divisibleBy8(self, num) :
        #  Shift right by 3
        auxiliary = num >> 3
        #  Shift left by 3
        auxiliary = auxiliary << 3
        #  Check if auxiliary is equal to number or not
        if (auxiliary == num) :
            print("\n Number", num ,"divisible by 8", end = "")
        else :
            print("\n Number", num ,"is not divisible by 8", end = "")
        
    

def main() :
    task = Divisibility()
    #  Test Inputs
    task.divisibleBy8(56)
    task.divisibleBy8(31)
    task.divisibleBy8(32)
    task.divisibleBy8(72)
    task.divisibleBy8(78)

if __name__ == "__main__": main()

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
#    Ruby program for
#    Number divisible by 8 using bitwise
class Divisibility 
    def divisibleBy8(num) 
        #  Shift right by 3
        auxiliary = num >> 3
        #  Shift left by 3
        auxiliary = auxiliary << 3
        #  Check if auxiliary is equal to number or not
        if (auxiliary == num) 
            print("\n Number ", num ," divisible by 8")
        else
 
            print("\n Number ", num ," is not divisible by 8")
        end

    end

end

def main() 
    task = Divisibility.new()
    #  Test Inputs
    task.divisibleBy8(56)
    task.divisibleBy8(31)
    task.divisibleBy8(32)
    task.divisibleBy8(72)
    task.divisibleBy8(78)
end

main()

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Scala program for
    Number divisible by 8 using bitwise
*/
class Divisibility()
{
    def divisibleBy8(num: Int): Unit = {
        // Shift right by 3
        var auxiliary: Int = num >> 3;
        // Shift left by 3
        auxiliary = auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            print("\n Number " + num + " divisible by 8");
        }
        else
        {
            print("\n Number " + num + " is not divisible by 8");
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Divisibility = new Divisibility();
        // Test Inputs
        task.divisibleBy8(56);
        task.divisibleBy8(31);
        task.divisibleBy8(32);
        task.divisibleBy8(72);
        task.divisibleBy8(78);
    }
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Swift 4 program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
    func divisibleBy8(_ num: Int)
    {
        // Shift right by 3
        var auxiliary: Int = num >> 3;
        // Shift left by 3
        auxiliary = auxiliary << 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            print("\n Number ", num ," divisible by 8", terminator: "");
        }
        else
        {
            print("\n Number ", num ," is not divisible by 8", terminator: "");
        }
    }
}
func main()
{
    let task: Divisibility = Divisibility();
    // Test Inputs
    task.divisibleBy8(56);
    task.divisibleBy8(31);
    task.divisibleBy8(32);
    task.divisibleBy8(72);
    task.divisibleBy8(78);
}
main();

Output

 Number  56  divisible by 8
 Number  31  is not divisible by 8
 Number  32  divisible by 8
 Number  72  divisible by 8
 Number  78  is not divisible by 8
/*
    Kotlin program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
    fun divisibleBy8(num: Int): Unit
    {
        // Shift right by 3
        var auxiliary: Int = num shr 3;
        // Shift left by 3
        auxiliary = auxiliary shl 3;
        // Check if auxiliary is equal to number or not
        if (auxiliary == num)
        {
            print("\n Number " + num + " divisible by 8");
        }
        else
        {
            print("\n Number " + num + " is not divisible by 8");
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Divisibility = Divisibility();
    // Test Inputs
    task.divisibleBy8(56);
    task.divisibleBy8(31);
    task.divisibleBy8(32);
    task.divisibleBy8(72);
    task.divisibleBy8(78);
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment