# Find most significant bit of a number X in base Y

Given two positive integers **X** and **Y**, the task is to find the MSB of X, in the given base Y.

**Examples:**

Input:X = 55, Y = 3

Output:2

Explanation:

55 is 2001 in base 3 with first digit as 2.

Input:X = 123, Y = 10

Output:1

Explanation:

123 is 123 in base 10 with first digit 1.

**Approach:** Let the task to find 1st digit of X = 1234 in base Y = 10, So to get First digit = 1:

Divide 1234 by 1000

=X / 10^{3}

=X / 10^{Number of Digits in X – 1}

=X / 10(which is for base 10)^{log(X) / log(10)}

For any other base, we can replace 10 with Y. Therefore, we can calculate the first digit of a number **X** in base **Y** by using the formula:

X / Y^{(log(X)/log(Y))}

Below is the implementation of the above approach:

## C++

`// C++ Program to find the ` `// first digit of X in base Y ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the first ` `// digit of X in base Y ` `void` `first_digit(` `int` `x, ` `int` `y) ` `{ ` ` ` `// calculating number of digits of ` ` ` `// x in base y ` ` ` `int` `length = ` `log` `(x) / ` `log` `(y) + 1; ` ` ` ` ` `// finding first digit of x in base y ` ` ` `int` `first_digit = x / ` `pow` `(y, length - 1); ` ` ` ` ` `cout << first_digit; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 55, Y = 3; ` ` ` ` ` `first_digit(X, Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the ` `// first digit of X in base Y ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to find the first ` `// digit of X in base Y ` `static` `void` `first_digit(` `int` `x, ` `int` `y) ` `{ ` ` ` `// calculating number of digits of ` ` ` `// x in base y ` ` ` `int` `length = (` `int` `)(Math.log(x) / ` ` ` `Math.log(y) + ` `1` `); ` ` ` ` ` `// finding first digit of x in base y ` ` ` `int` `first_digit = (` `int` `)(x / Math.pow(y, ` ` ` `length - ` `1` `)); ` ` ` ` ` `System.out.println(first_digit); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `X = ` `55` `, Y = ` `3` `; ` ` ` ` ` `first_digit(X, Y); ` `} ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the ` `# first digit of X in base Y ` `import` `math ` ` ` `# Function to find the first ` `# digit of X in base Y ` `def` `first_digit(x, y): ` ` ` ` ` `# Calculating number of digits of ` ` ` `# x in base y ` ` ` `length ` `=` `int` `(math.log(x) ` `/` ` ` `math.log(y) ` `+` `1` `) ` ` ` ` ` `# Finding first digit of x in base y ` ` ` `first_digit ` `=` `x ` `/` `math.` `pow` `(y, length ` `-` `1` `) ` ` ` ` ` `print` `(` `int` `(first_digit)) ` ` ` `# Driver code ` `X ` `=` `55` `Y ` `=` `3` ` ` `first_digit(X, Y) ` ` ` `# This code is contributed by ishayadav181 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the ` `// first digit of X in base Y ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the first ` `// digit of X in base Y ` `static` `void` `first_digit(` `int` `x, ` `int` `y) ` `{ ` ` ` `// calculating number of digits of ` ` ` `// x in base y ` ` ` `int` `length = (` `int` `)(Math.Log(x) / ` ` ` `Math.Log(y) + 1); ` ` ` ` ` `// finding first digit of x in base y ` ` ` `int` `first_digit = (` `int` `)(x / Math.Pow(y, ` ` ` `length - 1)); ` ` ` ` ` `Console.Write(first_digit); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `X = 55, Y = 3; ` ` ` ` ` `first_digit(X, Y); ` `} ` `} ` ` ` `// This code is contributed by Akanksha_Rai ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Given a number N in decimal base, find number of its digits in any base (base b)
- Find the Nth digit from right in base B of the given number in Decimal base
- Given a number N in decimal base, find the sum of digits in any base B
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Find the length of factorial of a number in any given base
- Find the Largest N digit perfect square number in Base B
- Round-off a number to a given number of significant digits
- Program to find the last digit of X in base Y
- Add two integers of different base and represent sum in smaller base of the two
- Check if a number is in given base or not
- Check if a N base number is Even or Odd
- Complement of a number with any base b
- Pandigital number in a given base
- Find the total count of numbers up to N digits in a given base B
- Largest value of x such that ax
^{x}is N-digit number of base b - Largest N digit number in Base B
- Check if a number N starts with 1 in b-base
- Check whether a number has consecutive 0's in the given base or not
- Number System and Base Conversions
- Find the height of a right-angled triangle whose area is X times its base

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.