Related Articles

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

• Last Updated : 20 Apr, 2021

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:
Explanation:
55 is 2001 in base 3 with first digit as 2.
Input: X = 123, Y = 10
Output:
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 / 103
= X / 10Number of Digits in X – 1
= X / 10log(X) / log(10) (which is for base 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 ``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;``}`

## 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`

## 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`

## 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`

## Javascript

 ``
Output:
`2`

Time Complexity: O(1)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up