×

Convert a number from base 2 to base 6

Base 2

Base 2 is the format where you can represent any number with just two values which are 0 and 1. With the help of 0s and 1s, we can represent any number we want. It is also called Binary format.

Base6

Base 6 is the format where we can use 6 digits (0 to 5) to represent any number. Base 6 is generally not used, while base 2 (binary) is used in computers and complete electronics.

In real life, we deal with the base 10 format, which is also called decimal format, where to represent any number; we use 10 digits (0 to 9).

To convert any number from base 2 to base 6, we will follow the following steps:

1. Convert the number into base 10 (decimal) from base 2(binary)

2. Now convert the number from base 10 to base 6.

For example:

We have a base 2 number N=(1100101)2, and our task is to convert this N into base 6.

Step1: 

From LSB (Least Significant Bit), add each number, and it’s multiplied by 2 to the power of its index.

          N In decimal would be: (1X26)+(1X25)+(0X24)+(0X23)+(1X22)+(0X21)+(1X20)

          So N = (101)10

Step2:

Now convert this 101 into an equivalent base 6 number by the dividing method.

          ans=0

          101%6=5 so add 5 into ans in LSB ,Now ans= 5

          Value => 101/6=16

          Now 16%6=4 so ad d4 into ans in LSB, Now ans=45

          value=>   16/6=2

          Since the value is lesser than 5, just add into ans in lSB and stop so ans=245

So finally, our base 6 would be = (245).

We will see the JAVA code for the following conversion:

Java Code:

class HelloWorld {
    public static void main(String[] args) {
       System.out.println(base10Tobase6(base2Tobase10("1100101")));
    }
    public static int base2Tobase10(String binary){
        
        int n=binary.length();
        int ans=0;
        for(int i=0;i<n;i++){
            char ch=binary.charAt(i);
            if(ch=='1')
            ans+=Math.pow(2,n-i-1);
        }
        return ans;
    }
    
    public static String base10Tobase6(int decimal){
        int ans=0;
        int idx=0;
        while(decimal>0){
            ans+=(decimal%6)*Math.pow(6,idx++);
            decimal/=6;
        }
        return ans+"";
    }
}


If we discuss the time complexity and space complexity, then we will notice that the first function, which converts the N length string of binary numbers into an equivalent decimal number, has a time complexity of O(N). and the second function, which converts the decimal integer to base 6 string, has the time complexity of O(M), where M is the length of the bits in base 6 format. For any number, N will always be greater than and equal to M.

So finally, time complexity would be: O(N).

Space taken by the first function is O(1), as it takes only one integer variable to return its answer. The second function takes the O(M) space of the string to store the result of the base 6 format.

So space complexity would be: O(M).

We can reverse the above steps to convert the number from base 2 to base 6.

Step1: convert from base 6 to base 10

Step2: convert from base 10 to base 2

Example: 245 we can convert it into decimal which will be

          2X62+4X61+5X60, so the decimal equivalent will be equal to 101.

Step2 is to convert 101 to binary using the divide method by 2.

          value=101 ans=””.

          ans=(101)%2 ans value=101/2 so value=50 and ans=1

          ans=50%2 in MSB and value = 50/2 , so value=25 and ans=01

          ans=25%2 in MSB and value = 25/2 , so value=12 and ans=101

          ans=12%2 in MSB and value = 12/2 , so value=6 and ans=0101

          ans=6%2 in MSB and value = 6/2 , so value=3 and ans=00101

          ans=3%2 in MSB and value = 3/2 , so value=1 and ans=100101

ans=1%2 in MSB and value = 1/2 , so value=0 and ans=1100101

As the value becomes 0 so, we will stop, and 1100101 is binary equivalent to 245 of base 6.

Java Code:

class HelloWorld {
    public static void main(String[] args) {
       System.out.println(base10Tobase2(base6Tobase10("245")));
    }
    public static int base6Tobase10(String base6){
        
        int n=base6.length();
        int ans=0;
        for(int i=0;i<n;i++){
            char ch=base6.charAt(i);
            ans+=(ch-'0')*Math.pow(6,n-i-1);
        }
        return ans;
    }
    
    public static String base10Tobase2(int decimal){
        String ans="";
        while(decimal>0){
            ans=(decimal%2)+ans;
            decimal/=2;
        }
        return ans;
    }
}

If we discuss the time complexity and space complexity, then we will notice that the first function, which converts the N length string of base 6 numbers into an equivalent decimal number, has a time complexity of O(M). The second function, which converts the decimal integer to a base 2 string, has the time complexity of O(N), where M is the length of the bits in base 6 format. For any number, N will always be greater than and equal to M.

So finally, time complexity would be: O(N).

Space taken by the first function is O(1), as it takes only one integer variable to return its answer. The second function takes the O(N) space of the string to store the result of the base 2 format.

So space complexity would be: O(N).


Related Topics

8085 Pin Configuration

Introduction: Intel 8085 is consist 40 pin IC. The pin name of 8085 is A8 – A15; AD0 – AD7; ALE; IO/M; S0, S1; RD; WR; READY; HOLD; HLDA; INTR; INTA;...

6 minutes read.

Ethernet Frame Format

What is Ethernet? As we know in computer networking, LAN (Local Area Network) is used to connect devices in limited geographical areas like schools or a building.LAN uses different topologies (arrangement...

3 minutes read.

Convert a number from base 2 to base 6

Base 2 Base 2 is the format where you can represent any number with just two values which are 0 and 1. With the help of 0s and 1s, we can...

4 minutes read.

8255 Microprocessor Operating Modes

8255 microprocessor was invented by intel in the year 1980s. 8255 microprocessor is a general-purpose microprocessor that can be programmed. 8255 is mostly used as an interface between devices like...

3 minutes read.

8085 instructions set

Introduction: In the computer, the user sends the data. The computer can use data and it also process the data. After the processing, send the result to the user. The specific...

10 minutes read.

74138 IC DECODER

74138 DECODER Decoder A combinational circuit called a decoder can have up to 2n output lines and 'n' input lines. When a decoder is enabled, depending on the mix of inputs,...

4 minutes read.

Flag Register of 8086 Microprocessor

In this article, we are going to study about the one of the registers named flag registers of 8086 microprocessor. We are also going to know where and how to...

3 minutes read.

8085 Arithmetic Instructions

Arithmetic instructions are a type of instruction in a computer's instruction set that perform arithmetic operations on data. These operations can include addition, subtraction, multiplication, and division. Arithmetic instructions can...

4 minutes read.

Bipartite Graph

A graph is said to be bipartite if and only if we can divide all the vertices of a graph into two sets such that these sets are mutually exclusive...

4 minutes read.

IEEE Standard 754 Floating Point Numbers

Floating Point Representation: IEEE- 754 There were many problems in the conventional representation of floating-point notation like we could not express 0(zero), infinity number. To solve this, scientists have given a...

8 minutes read.

CarryLook Ahead Adder

What is Adder? Adder is the combinational circuit that is used to add the bits in digital electronics. As we know, in the digital system, every number is represented in the...

4 minutes read.

CPU Registers

CPU Registers Within the CPU, we have small- small storage units called registers. If we are executing a certain program within the CPU, there's a need to store that program content,...

11 minutes read.

Data Path, ALU and Control Unit

Data Path, ALU(Arithmetic Logic Unit) and Control Unit What is the Data path? Suppose any data processing operation should be performed in the CPU like transferring the content of register from one...

5 minutes read.

Control Unit Organization

Control Unit Organization The Control Unit is the unit in the CPU, which controls the various components like input  & output devices, logic unit and memory. The Control Unit is the...

7 minutes read.

BCD to 7 Segment Decoder

What is BCD? BCD is called Binary Coded Decimal. As you know there are a lot of forms in which we can represent the number like Binary, decimal, hexadecimal, and octal...

3 minutes read.

Instruction Pipelining and Pipeline Hazards

Instruction Pipelining and Pipeline Hazards To apply pipeline, the prerequisite is to perform the same processing over multiple inputs, then pipelining is important. So instruction execution is the best example for...

10 minutes read.

Basic Terminologies Related to COA

Some Basic Terminologies Related to COA Computer organization and architecture is the detailed study of the internal components of the computer with their functionality. It describes how components arranged together and...

7 minutes read.

Addressing Modes

Introduction: Addressing Modes An instruction consists of two parts opcode and operands. Opcode tells the operation going to perform, and operand information is the address of the operand. The CPU interprets...

8 minutes read.

Addressing mode in 8085 microprocessor

What is a Microprocessor? Microprocessor is a unit which is able to perform arithmetic and logical operations and is built on a single chip. The 8085 microprocessor is built by Intel which...

4 minutes read.

Machine Instructions

What is a Digital Computer? A computer that takes input in a binary form and produces output in binary form. The question arises of what kind of input the computer takes...

4 minutes read.