Given a number n, check if it is divisible by 8 using bitwise operators.
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 .
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a number is divisible by 17 using bitwise operators
- Check if a Number is Odd or Even 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
- Bitwise Operators in C/C++
- Check if a number is positive, negative or zero using bit operators
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Bitwise right shift operators in Java
- What are the differences between bitwise and logical AND operators in C/C++?
- Toggle case of a string using Bitwise Operators
- Russian Peasant (Multiply two numbers using bitwise operators)
- Check if a number is divisible by 23 or not
- Check if a number is divisible by 31 or not
- Check if the number is divisible 43 or not
- Check if the given number is divisible by 71 or not
- Check if a number is divisible by 41 or not
- Check if a number is divisible by 47 or not
- Check if a number is divisible by all prime divisors of another number
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.