- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to round up the next highest power of 2.
Examples
For example, n = 12 => Next number power of 2 is 16.
For example, n = 20 => Next number power of 2 is 32.
Approach to solve this problem
Step 1 − Define method, that accepts a number n.
Step 2 − Iterate k := 1 until k < n.
Step 3 − In a loop, calculate k << 1.
Step 4 − At the end, return k.
Example
package main import "fmt" func NextPowOf2(n int) int{ k := 1 for ;k < n; { k = k << 1 } return k } func main(){ fmt.Printf("Round of highest power of 2 for %d is %d.\n", 20, NextPowOf2(20)) fmt.Printf("Round of highest power of 2 for %d is %d.\n", 16, NextPowOf2(16)) fmt.Printf("Round of highest power of 2 for %d is %d.\n", 131, NextPowOf2(131)) }
Output
Round of highest power of 2 for 20 is 32. Round of highest power of 2 for 16 is 16. Round of highest power of 2 for 131 is 256.
- Related Articles
- Golang Program to round up the next previous power of 2.
- Find the highest power of 2 in 1440.
- Golang Program to Calculate The Power using Recursion
- Golang Program to check the power of 4 of a given number
- Find the highest power of 5 in 23750.
- Round number down to nearest power of 10 JavaScript
- Golang program to calculate the sum of all odd numbers up to N
- How to round up to the nearest N in JavaScript
- Program to find Reordered Power of 2 in Python
- How to round up a number in JavaScript?
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- Program to find out the value of a power of 2 in Python
- Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Haskell Program to calculate the Highest Common Factor

Advertisements