- 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 turn on the k’th bit in a number.

## Example

For example consider n = 20(00010100), k = 4. So result after turning on 4^{th}bit => 00010000 | (1 << (4-1))

**Approach to solve this problem**

**Step 1** − Define a method, where **n** and **k** would be the arguments, returns type is **int**.

**Step 2** − Perform AND operation with **n** | (1<<(k-1)).

**Step 3** − Return the obtained number.

## Example

package main import ( "fmt" "strconv" ) func TurnOnKthBit(n, k int) int { return n | (1 << (k-1)) } func main(){ var n = 20 var k = 4 fmt.Printf("Binary of %d is: %s.\n", n, strconv.FormatInt(int64(n), 2)) newNumber := TurnOnKthBit(n, k) fmt.Printf("After turning on %d th bit of %d is: %d.\n", k, n, newNumber) fmt.Printf("Binary of %d is: %s.\n", newNumber, strconv.FormatInt(int64(newNumber), 2)) }

## Output

Binary of 20 is: 10100. After turning on 4 th bit of 20 is: 28. Binary of 28 is: 11100.

- Related Questions & Answers
- Golang program to turn off the k’th bit in a number.
- Golang program to check if k’th bit is set for a given number or not.
- Position of the K-th set bit in a number in C++
- Find value of k-th bit in binary representation in C++
- K’th Boom Number in C++
- Check whether K-th bit is set or nots in Python
- Golang Program to find the position of the rightmost set bit
- Golang Program to Count the Number of Digits in a Number
- k-th prime factor of a given number in java
- Write a Golang program to reverse a number
- 8085 Program to Divide a 16-bit number by an 8-bit number
- 8086 program to divide a 16 bit number by an 8 bit number
- Find the k-th smallest divisor of a natural number N in C++
- Golang Program to find the parity of a given number.
- Program to find the K-th last node of a linked list in Python

Advertisements