Open In App

Check if a number is divisible by 17 using bitwise operators

Last Updated : 07 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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%16 
Below is the implementation of the above approach: 
 

CPP




// 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 than 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;
}


Java




// 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 than 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


Python3




# 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 than 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


C#




// 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 than 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


PHP




<?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 than 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
?>


Javascript




<script>
 
// JavaScript 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 than 17, not
    // divisible by 17
    if (n < 17)
        return false;
 
    // reducing the number by floor(n/16)
    // - n%16
    return isDivisibleby17(Math.floor(n >> 4) - Math.floor(n & 15));
}
 
// driver code to check the above function
 
    let n = 35;
    if (isDivisibleby17(n))
        document.write(n + " is divisible by 17");
    else
        document.write(n + " is not divisible by 17");
 
// This code is contributed by Surbhi Tyagi.
 
</script>


Output: 

35 is not divisible by 17

 Time Complexity: O(log16N), as we are using recursion and in each call we are decrementing by division of 16.

Auxiliary Space: O(1), as we are not using any extra space.



Previous Article
Next Article

Similar Reads

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 Recommended PracticeCheck if a number is divisible by 8Try It! Approach: Result = (((n &gt;&gt; 3) &lt;&lt; 3) == n). First we shift the 3 bit right then we shift the 3 bit left and then compare the number with the given nu
4 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: Odd Input: 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 last bit of the number is set or not. If the las
11 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 operators based methods and require use of % and /. The
6 min read
Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that: cntAND: Count of pairs where bitwise AND of least significant bits is 1.cntOR: Count of pairs where bitwise OR of least significant bits is 1.cntXOR: Count of pairs where bitwise XOR of least significant bits is 1. Exampl
7 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND
Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Explanation:Step 1: Select the pair {12, 15} and replac
9 min read
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers. Examples: Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A &amp; B = 2, then the possible value of A and B is 3 and 6 respe
7 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 to 0, if divisible by 4 then two LSB's set to 0, if
4 min read
Check if any Array pair has bitwise XOR greater than bitwise AND
Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] ⊕ arr[j] &gt; arr[i] &amp; arr[j], (0 ≤ i &lt; j ≤ N-1) where ⊕ represents the Bitwise XOR operator and &amp; represents bitwise AND operator. Examples: Input: arr[] = {4, 5, 8, 6}Output:
9 min read
Check if an Array exists with Bitwise OR as A and Bitwise AND as B
Three integers N, A and B are given to you. you need to create an array arr of length 'N' such that it satisfies the following conditions : Each elements of arr should be unique.arr[1] | arr[2] | arr[3] |....arr[N] = A arr[1] &amp; arr[2] &amp; arr[3] &amp;....arr[N] = B Where '|' Represents Bitwise OR and '&amp;' Represents Bitwise AND. Your task
8 min read
Multiply two integers without using multiplication, division and bitwise operators, and no loops
By making use of recursion, we can multiply two integers with the given constraints. To multiply x and y, recursively add x y times. Approach: Since we cannot use any of the given symbols, the only way left is to use recursion, with the fact that x is to be added to x y times. Base case: When the numbers of times x has to be added becomes 0. Recurs
9 min read
Article Tags :
Practice Tags :