Given an unsigned integer x. Round it up to the next greater multiple of 8 using bitwise operations only.

**Examples:**

Input : 35 Output : 40 Input : 64 Output : 64 (As 64 is already a multiple of 8. So, no modification is done.)

**Solution 1:** We first add 7 and get a number x + 7, then we use the technique to find next smaller multiple of 8 for (x+7). For example, if x = 12, we add 7 to get 19. Now we find next smaller multiple of 19, which is 16.

**Solution 2:** An efficient approach to solve this problem using bitwise AND operation is:

x = (x + 7) &(-8)

This will round up x to the next greater multiple of 8.

## C++

`// CPP program to find smallest greater multiple ` `// of 8 for a given number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns next greater multiple of 8 ` `int` `RoundUp(` `int` `& x) ` `{ ` ` ` `return` `((x + 7) & (-8)); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `x = 39; ` ` ` `cout << RoundUp(x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find smallest ` `// greater multiple of 8 for ` `// a given number ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `// Returns next greater ` `// multiple of 8 ` `class` `GFG ` `{ ` ` ` `static` `int` `RoundUp(` `int` `x) ` ` ` `{ ` ` ` `return` `((x + ` `7` `) & (-` `8` `)); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `x = ` `39` `; ` ` ` `System.out.println(RoundUp(x)); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find ` `# smallest greater multiple ` `# of 8 for a given number ` ` ` `# Returns next greater ` `# multiple of 8 ` `def` `RoundUp(x): ` ` ` `return` `((x ` `+` `7` `) & (` `-` `8` `)) ` ` ` `# Driver Code ` `x ` `=` `39` `print` `(RoundUp(x)) ` ` ` `# This code is contributed ` `# by prerna saini ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find smallest ` `// greater multiple of 8 for ` `// a given number ` `using` `System; ` ` ` `// Returns next greater ` `// multiple of 8 ` `class` `GFG ` `{ ` ` ` `static` `int` `RoundUp(` `int` `x) ` ` ` `{ ` ` ` `return` `((x + 7) & (-8)); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `x = 39; ` ` ` `Console.WriteLine(RoundUp(x)); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by SoumikMondal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find smallest greater ` `// multiple of 8 for a given number ` ` ` `// Returns next greater ` `// multiple of 8 ` `function` `RoundUp(` `$x` `) ` `{ ` ` ` `return` `((` `$x` `+ 7) & (-8)); ` `} ` ` ` `// Driver Code ` `$x` `= 39; ` `echo` `RoundUp(` `$x` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

40

**Time Complexity:** O(1)

**Space Complexity:** O(1)

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.

## Recommended Posts:

- Round to next smaller multiple of 8
- Puzzle | 3 cuts to cut round cake into 8 equal pieces
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Efficiently check whether n is a multiple of 4 or not
- Find the multiple of x which is closest to a^b
- Find the largest multiple of 2, 3 and 5
- Transition shorthand with multiple properties in CSS?
- Find the largest multiple of 3 | Set 1 (Using Queue)
- How to get multiple selected values of select box in php?
- Smallest multiple of 3 which consists of three given non-zero digits
- Concept behind Multiple Booting Guide
- Check if binary string multiple of 3 using DFA
- Assigning multiple characters in an int in C language
- How to select multiple columns in a pandas dataframe
- Count of integers in an Array whose length is a multiple of K
- How to drop one or multiple columns in Pandas Dataframe
- Python | Multiple Face Recognition using dlib
- Pair of integers with difference K having an element as the K-th multiple of the other
- Count of elements in an Array whose set bits are in a multiple of K
- Check if a number is multiple of 9 using bitwise operators

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.