Skip to content
Related Articles

Related Articles

Check if a number is divisible by 17 using bitwise operators
  • Difficulty Level : Hard
  • Last Updated : 18 Apr, 2018

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, we must rewrite the expression in powers of 2.



n/17 = (16*n)/(17*16)
     = (17 - 1)*n/(17*16)
     = (n/16) - (n/(17*16))

We can rewrite n/16 as floor(n/16) + (n%16)/16 using general rule of division.

n/17 = floor(n/16) + (n%16)/16 - 
       (floor(n/16) + (n%16)/16)/17
     = floor(n/16) - (floor(n/16) - 
            17*(n%16)/16 + (n%16)/16)/17
     = floor(n/16) - (floor(n/16)-n%16)/17

The left-hand-side of this equation is n/17. That will be an integer only when the right-hand-side is an integer. floor(n/16) is an integer by definition. So the whole left-hand-side would be an integer if (floor(n/16)-n%16)/17 is also an integer.

This implies n is divisible by 17 if (floor(n/16)-n%16) is divisible by 17.

(floor(n/16)-n%16) can be written in bitwise as (int)(n>>4) – (int)(n&15) where n>>4 means n/16 and n%15 means n%15

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check if a number is
// divisible by 17 or not using bitwise
// operator.
#include <bits/stdc++.h>
using namespace std;
  
// function to check recursively if the
// number is divisible by 17 or not
bool isDivisibleby17(int n)
{
    // if n=0 or n=17 then yes
    if (n == 0 || n == 17)
        return true;
  
    // if n is less then 17, not
    // divisible by 17
    if (n < 17)
        return false;
  
    // reducing the number by floor(n/16)
    // - n%16
    return isDivisibleby17((int)(n >> 4) - (int)(n & 15));
}
  
// driver code to check the above function
int main()
{
    int n = 35;
    if (isDivisibleby17(n))
        cout << n << " is divisible by 17";
    else
        cout << n << " is not divisible by 17";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if a number is
// divisible by 17 or not using bitwise
// operator.
class GFG{
      
    // function to check recursively if the
    // number is divisible by 17 or not
    static boolean isDivisibleby17(int n)
    {
          
        // if n=0 or n=17 then yes
        if (n == 0 || n == 17)
            return true;
      
        // if n is less then 17, not
        // divisible by 17
        if (n < 17)
            return false;
      
        // reducing the number by 
        // floor(n/16) - n%16
        return isDivisibleby17((int)(n >> 4)
                            - (int)(n & 15));
    }
      
    // driver function
    public static void main(String[] args)
    {
        int n = 35;
        if (isDivisibleby17(n) == true)
            System.out.printf
            ("%d is divisible by 17",n);
        else
            System.out.printf
            ("%d is not divisible by 17",n);
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to
# check if a number is
# divisible by 17 or
# not using bitwise
# operator.
  
# function to check recursively if the
# number is divisible by 17 or not
def isDivisibleby17(n):
  
    # if n=0 or n=17 then yes
    if (n == 0 or n == 17):
        return True
  
    # if n is less then 17, not
    # divisible by 17
    if (n < 17):
        return False
  
    # reducing the number by floor(n/16)
    # - n%16
    return isDivisibleby17((int)(n >> 4) - (int)(n & 15))
  
  
# driver code to check the above function
n = 35
if (isDivisibleby17(n)):
    print(n,"is divisible by 17")
else:
    print(n,"is not divisible by 17")
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if a number is
// divisible by 17 or not using bitwise
// operator.
using System;
  
class GFG
{
      
    // function to check recursively if the
    // number is divisible by 17 or not
    static bool isDivisibleby17(int n)
    {
          
        // if n=0 or n=17 then yes
        if (n == 0 || n == 17)
            return true;
      
        // if n is less then 17, not
        // divisible by 17
        if (n < 17)
            return false;
      
        // reducing the number by 
        // floor(n/16) - n%16
        return isDivisibleby17((int)(n >> 4)
                            - (int)(n & 15));
    }
      
    // Driver function
    public static void Main()
    {
        int n = 35;
        if (isDivisibleby17(n) == true)
            Console.WriteLine
            (n +"is divisible by 17");
        else
            Console.WriteLine
            ( n+ " is not divisible by 17");
    }
}
  
// This code is contributed by
// vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to check if a 
// number is divisible by 17
// or not using bitwise
// operator.
  
// function to check recursively 
// if the number is divisible 
// by 17 or not
function isDivisibleby17($n)
{
      
    // if n=0 or n=17 then yes
    if ($n == 0 || $n == 17)
        return true;
  
    // if n is less then 17, not
    // divisible by 17
    if ($n < 17)
        return false;
  
    // reducing the number by floor(n/16)
    // - n%16
    return isDivisibleby17((int)($n >> 4) - 
                            (int)($n & 15));
}
  
    // Driver Code
    $n = 35;
    if (isDivisibleby17($n))
        echo $n." is divisible by 17";
    else
        echo $n." is not divisible by 17";
  
// This code is contributed by mits 
?>

chevron_right



Output:

35 is not divisible by 17

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :