×

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

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.

Micro-Operations

Micro-Operations If CPU wants to perform any operation, suppose execution of only one instruction or even break it down one particular execution phase (like fetch instruction, write back, decode, operand fetch...

9 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.

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.

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.

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.

Pipelining: Computer Organization and Architecture

Introduction to Pipelining Before learning pipeline, we have to understand about the Parallel processing. Parallel processing is the processing of data simultaneously. There are three techniques of parallel processing. Types of Processing: Vector ProcessingArray...

7 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.

Assembly Language Register

Assembly Language Register Assembly language was introduced during the late 1940s in the second generation. It is a low-level language that helps in communication between the user and the computer. It...

6 minutes read.

Types of Microprocessors

Introduction:Microprocessor is known as the computer processor. The IC or integrated Circuit makes the microprocessor. That is the brain of the computer machine.  The microprocessor is the Processor or CPU (Central...

3 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.

Advantages and Disadvantages of Flash Memory

What is Flash Memory? Electrically Erasable Programmable Read-Only Memory (EEPROM) and Erasable Programmable Read-Only Memory (EPROM) are used for flash memory (EEPROM). Although technically the word flash is used in the industry...

3 minutes read.

8086 Microprocessor Pin Configurations

8086 microprocessor is invented and developed by Intel in 1976. The 8086 Microprocessor is an enhanced version of the 8085 Microprocessor. There are 20 address lines and 16 data lines...

6 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.

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.

Digital Number System

Digital Number System: The computer understands digital language only. Many number systems are used in digital technology. The most common are Decimal, Binary, Octal and Hexadecimal number systems. Types of the...

7 minutes read.

Control Signals in 8155 Microprocessor

Intel designed the chip 8155 used to interface with I/O devices. Basically, the 8085 microprocessor is directly unable to deal with its peripheral input output devices for reading data or...

3 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.

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.

COA: Interrupt and its types

Introduction: In general terms, the word interrupt means to stop the progress of ongoing work in between or to break the continuation of the work. In early digital computing, the...

5 minutes read.