

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 find the position of the rightmost set bit
Examples
Consider n = 20(00010100)
Now return log2(20 & -20) => 2+1 => 3
Approach to solve this problem
Step 1 − Define a method, where n and is an argument, return type is int.
Step 2 − Return log2(n & -n)+1.
Example
package main import ( "fmt" "math" "strconv" ) func FindRightMostSetBit(n int) int { if (n & 1) != 0{ return 1 } return int(math.Log2(float64(n & -n))) + 1 } func main(){ var n = 20 fmt.Printf("Binary of %d is: %s.\n", n, strconv.FormatInt(int64(n), 2)) fmt.Printf("Position of the rightmost set bit of the given number %d is %d.\n", n, FindRightMostSetBit(n)) }
Output
Binary of 20 is: 10100. Position of the rightmost set bit of the given number 20 is 3.
- Related Questions & Answers
- Position of rightmost set bit in C++
- Python Program to Clear the Rightmost Set Bit of a Number
- Position of rightmost different bit in C++
- Find position of the only set bit in C++
- Position of rightmost common bit in two numbers in C++
- 8085 program to find the set bit of accumulator
- Position of rightmost bit with first carry in sum of two binary in C++
- Position of the K-th set bit in a number in C++
- Set the bit at a specific position in the BitArray to the specified value in C#?
- Golang Program to find the parity of a given number.
- Golang Program to Find the Smallest Divisor of an Integer
- Golang program to turn off the k’th bit in a number.
- Golang Program to turn on the k’th bit in a number.
- Write a Golang program to find odd and even numbers using bit operation
- Golang Program to count the set bits in an integer.
Advertisements