How to usebitwise operators to convert decimal numbers to hexadecimal. in Python

Steps:

  1. Take the input N from the user.
  2. Initialize a variable hex_digits as a string containing all the hexadecimal digits.
  3. Iterate from 1 to N.
    • For each number, initialize an empty string ‘hex_value’.
    • If the number is greater than 0, then perform the following steps:
      a. Calculate the remainder when the number is divided by 16.
      b. Add the corresponding hexadecimal digit from hex_digits to the beginning of hex_value.
      c. Right shift the number by 4 bits.
  4. Print the hexadecimal value for each number.

Below is the implementation of the above approach:

Python3




# Python program for the above approach
 
# Function to print the hexadecimal number
# from 1 to N
def printHexadecimalNumber(N):
    hex_digits = "0123456789ABCDEF"
 
    # Iterate over the range
    for num in range(1, N+1):
        hex_value = ""
 
        while num > 0:
            rem = num % 16
            hex_value = hex_digits[rem] + hex_value
 
            # Shift the number by 4 bits
            num >>= 4
 
        # Print the hex value
        print(hex_value)
 
 
# Driver Code
N = 11
 
printHexadecimalNumber(N)


Output

1
2
3
4
5
6
7
8
9
A
B

Time Complexity: O(N*log N)
Auxiliary Space: O(1)



Python program to print the hexadecimal value of the numbers from 1 to N

Given a number N, the task is to write a Python program to print the hexadecimal value of the numbers from 1 to N.

Examples:

Input: 3
Output: 1
        2
        3

Input: 11
Output: 1
        2
        3
        4
        5
        6
        7
        8
        9
        a
        b

Approach:

  • We will take the value of N as input.
  • Then, we will run the for loop from 1 to N+1 and traverse each “i” through the hex() function.
  • Print each hexadecimal value.

Note: The hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form.

Below are the implementations based on the above approach:

Python3




# Python program to print the hexadecimal value of the
# numbers from 1 to N
 
# Function to find the hexadecimal value of the numbers
# in the range 1 to N
def hex_in_range(n):
     
    # For loop traversing from 1 to N (Both Inclusive)
    for i in range(1, n+1):
         
        # Printing hexadecimal value of i
        print(hex(i)[2:])
 
# Calling the function with input 3
print("Input: 3")
hex_in_range(3)
 
# Calling the function with input 11
print("Input: 11")
hex_in_range(11)


Output

Input: 3
1
2
3
Input: 11
1
2
3
4
5
6
7
8
9
a
b

Time complexity : O(N), as it performs a loop operation for every number from 1 to N, and the time taken increases linearly with the input value N.

Space complexity : O(1), as it only uses a constant amount of memory regardless of the input value N.

Similar Reads

Approach: String formatting approach to convert numbers to hexadecimal

...

Method: Manual conversion method

Take input from the user using the input function and convert it to an integer using the int function. This gives us the value of N. Loop through the numbers from 1 to N using the range function. Inside the loop, use string formatting to convert each number to its hexadecimal value. To do this, use the following expression: “{0:x}”.format(i). This expression formats the value of i as a lowercase hexadecimal string. Print the hexadecimal value using the print function. Here, the {0:x} part in the string formatting expression is a placeholder that represents the value of i. The 0 indicates the index of the value to be formatted (in this case, the first value in the parentheses), and the x specifies that the value should be formatted as a lowercase hexadecimal string....

Approach: Using bitwise operators to convert decimal numbers to hexadecimal.

...