Challenge 1: Get the First Set Bit Position Using the Left Shift
This problem is similar to the last lesson we discussed. If you need a clue, return to the previous lesson to further your understanding.
Table of Contents
This problem is similar to the last lesson we discussed. If you need a clue, return to the previous lesson to further your understanding.
Introduction
In this question, we need to find the first set-bit position from the right.
Problem statement
Given an input number, find the first set-bit position of the number.
Input: n = 18
Output: 2
Coding exercise
This problem is designed for your practice, so try to solve it yourself first. You can always refer to the solution in the next lesson if you get stuck. Good luck!
Hint: Use the previous logic we discussed to solve this.
// java
// TODO: finish the challenge or check next lesson for solution
class Solution {
public static int getFirstSetBitPos(int n) {
// Write - Your - Code- Here
return -1; // change this and return the position of the first set-bit.
}
}
# Python
# TODO: finish the challenge or check next lesson for solution
def getFirstSetBitPos(n):
# Write - Your - Code- Here
return -1 # change this and return the position of the first set-bit.
// javascript
// TODO: finish the challenge or check next lesson for solution
const getFirstSetBitPos = n => {
// Write - Your - Code- Here
return -1; // change this and return the position of the first set-bit.
}
// javascript
// TODO: finish the challenge or check next lesson for solution
#include <iostream>
using namespace std;
int getFirstSetbit(int n) {
// Write - Your - Code- Here
return -1; // change this and return the position of the first set-bit.
}
// typescript
// TODO: finish the challenge or check next lesson for solution
export const getFirstSetBitPos = (n: number): number => {
// Write - Your - Code- Here
return -1; // change this and return the position of the first set-bit.
}
The solution will be explained in the next lesson.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.