COA Tutorial
Basic CO and Design
Computer Instructions
Digital Logic Circuits
Map Simplification
Combinational Circuits
Flip - Flops
Digital Components
Register Transfer
Micro-Operations
Memory Organization
COA_Misc
- Booth's Multiplication Algorithm
- Branch Instruction in Computer Organization
- Data Representation in Computer Organization
- ALU and Data Path in Computer Organization
- External memory in Computer Organization
- Structured Computer Organization
- Types of Register in Computer Organization
- Secondary Storage Devices in Computer Organization
- Types of Operands in Computer Organization
- Serial Communication in Computer organization
- Addressing Sequencing in Computer Organization
- Simplified Instructional Computer (SIC)
- Arithmetic Instructions in AVR microcontroller
- Conventional Computing VS Quantum Computing
- Instruction set used in Simplified Instructional Computer
- Branch Instruction in AVR microcontroller
- Conditional Branch instruction in AVR Microcontroller
- Data transfer instruction in AVR microcontroller
- Difference between Memory-based and Register-based addressing modes
- Difference between 1's complement Representation and 2's complement Representation
- CALL Instructions and Stack in AVR Microcontroller
- Difference between Call and Jump Instructions
- Overflow in Arithmetic Addition in Binary number System
- Horizontal Micro-programmed Vs. Vertical Micro-programmed Control Unit
- Hardwired Vs. Micro-programmed Control Unit
- Non-Restoring Division Algorithm for Unsigned Integer
- Restoring Division Algorithm for Unsigned Integer
- Debugging a Machine-level Program
- Dependencies and Data Hazard in pipeline in Computer Organization
- Execution, Stages and Throughput in Pipeline
- Types of Pipeline Delay and Stalling
- Timing Diagram of MOV Instruction
- Advantages and Disadvantages of Flash Memory
- Importance/Need of negative feedback in amplifiers
- Anti-Aliasing - Computer Graphics
- Bus Arbitration in Computer Organization
- Convert a number from Base 2 (Binary) to Base 6
- Cache Coherence
- EHCI
- Cache Memory and Virtual Memory
- Electrical Potential and Potential Difference
- RAM and Cache
- SIM and RIM instructions in 8085 processor
- Clusters in Computer Organization
- Data Types and Addressing Modes of 80386/80386DX Microprocessor
Convert a number from Base 2 (Binary) to Base 6
The practical implementation of converting a binary number into a base 6 number is discussed in this article:
Problem - The problem is to convert a given binary number into its equivalent number in base 6.
For Example:
Input 1: N = 1101101 = 10910 Output = 301 (3016 = 3 x 62 + 0 x 61 + 1 x 60 = 3 x 36 + 1 = 109) Input 2: N = 1110111 = 11910
Output = 315 (3156 = 3 x 62 + 1 x 61 + 5 x 60 = 3 x 36 + 1 x 6 + 5 x 1 = 119)
Hello Java Program for Beginners
Idea and approach
The simple idea to get the desired result is to first convert the given binary into its equivalent number in base 10 and then convert it into its equivalent number in base 6.
Let us now understand the working/functions involved in finding the solution:
- Working of binaryToDecimal() function:
The pseudo-code of the required function is given below:
Binary To Decimal (Int n) // Where n is the binary input
- {
- a. Int dec = 0; // decimal variable to store the result of the function
- b. Int pv = 1; // Pv is the place value variable
- c. While n is greater than 0
- d. {
- e. dec = dec + pv x (n % 10)
- f. pv = pv x 10
- g. n = n / 10
- h. }
- i. decimalToBase6(dec) // Calling the Decimal to Base 6 function to get the output
- }
- Working of decimalToBase6() function:
The pseudo-code of the required function is given below:
Decimal To Base6 (Int dec) // Where dec is the decimal input
- {
- a. Int output = 0; // output variable to store the result of the function
- b. Int pv = 1; // Pv is the place value variable
- c. While dec is greater than 0
- d. {
- e. output = output + pv x (n % 6)
- f. pv = pv x 10
- g. dec = dev / 6
- h. }
- i. Print the output and exit
- }
Let's take an example to understand further:
Let n = 1101, when we call the binary to decimal function, it will first create some variables and initialize them with some value. Here, dec = 0, and pv = 1 is created. Now, check the while-loop condition. Here, n = 1101, which is greater than zero so, we move to the first iteration.
Iteration 1:
- dec = dec + pv * (n % 10) = 0 + 1 * (1) = 0 + 1 = 1
- pv = pv * 2 = 1 * 2 = 2
- n = n /10 = 1101 / 10
Iteration 2:
- dec = dec + pv * (n % 10) = 1 + 2 * (0) = 1 + 0 = 1
- pv = pv * 2 = 2 * 2 = 4
- n = n / 10 = 110 / 10 = 11
Iteration 3:
- dec = dec + pv * (n % 10) = 1 + 4 * (1) = 1 + 4 = 5
- pv = pv * 2 = 4 * 2 = 8
- n = n / 10 = 11 / 10 = 1
Iteration 4:
- dec = dec + pv * (n % 10) = 5 + 8 * (1) = 5 + 8 = 13
- pv = pv * 2 = 8 * 2 = 16
- n = n / 10 = 1 / 10 = 0
Here, the while-loop will terminate as n is now equal to zero. Now, it will call the decimalToBase6(dec) function and pass the dec = 13 to it.
When the decimaltToBase6 function is called, it creates pv = 1 and output = 0. The dec = 13 is passed to this function and it is greater than zero so the while-loop condition is satisfied. Let's see each iteration one by one:
Iteration 1:
- output = output + pv * (dec % 6) = 0 + 1 * (13 % 6) = 0 + 1 * 1 = 1
- pv = pv * 10 = 1 * 10 = 10
- dec = dec / 6 = 13 / 6 = 2
Iteration 2:
- output = output + pv * (dec % 6) = 1 + 10 (2 % 6) = 1 + 10 * 2 = 21
- pv = pv * 10 = 10 * 10 = 100
- dec = dec / 6 = 2 / 6 = 0
here, dec is now equal to zero. So, in the next iteration, the condition of the while-loop will fail and it will print the output and exit from the program.
Practical Implementation of the above problem
1. Java Implementation:
- package javatpoint;
- public class BinaryToBase6 {
- // Function To convert the decimal number into its equivalent number in base 6
- public static void decimalToBase6(int dec) {
- // Output variable will store the result
- int output = 0;
- int pv = 1;
- // The while loop terminates when the dec number becomes equal to zero
- while (dec > 0) {
- output = output + pv * (dec % 6);
- dec = dec / 6;
- pv = pv * 10;
- }
- System.out.println(output);
- }
- // Function to convert the binary number into its equivalent decimal number
- public static void binaryToDecimal(int n) {
- // The dec variable will store the decimal number
- int dec = 0;
- int pv = 1;
- int temp = n;
- // The while loop terminates when the temp (Binary number) becomes equal to zero
- while (temp > 0) {
- dec = dec + pv * (temp % 10);
- temp = temp / 10;
- pv = 2 * pv;
- }
- System.out.print("The Base6 equivalent of " + n + " = ");
- // Calling the function to convert the dec into base6 number
- decimalToBase6(dec);
- }
- public static void main(String[] args) {
- // Calling the function and passing a binary number with each function call
- binaryToDecimal(1101101);
- binaryToDecimal(1110111);
- binaryToDecimal(1010111);
- binaryToDecimal(1101111);
- }
- }
Sample Output:
The Base6 equivalent of 1101101 = 301 The Base6 equivalent of 1110111 = 315 The Base6 equivalent of 1010111 = 223 The Base6 equivalent of 1101111 = 303
2. C++ Implementation
- #include <iostream>
- using namespace std;
- // Function To convert a decimal number into its equivalent number in base 6
- void decimalToBase6(int dec)
- {
- // Output variable will store the result
- int output = 0;
- int pv = 1;
- // The while loop terminates when the dec number becomes equal to zero
- while (dec > 0)
- {
- output = output + pv * (dec % 6);
- dec = dec / 6;
- pv = pv * 10;
- }
- cout << output << endl;
- }
- // Function to convert a binary number into its equivalent decimal number
- void binaryToDecimal(int n)
- {
- // The dec variable will store the decimal number
- int dec = 0;
- int pv = 1;
- int temp = n;
- // The while loop terminates when the temp (Binary number) becomes equal to zero
- while (temp > 0)
- {
- dec = dec + pv * (temp % 10);
- temp = temp / 10;
- pv = 2 * pv;
- }
- cout << "The Base6 equivalent of " << n << " = ";
- // Calling the function to convert the dec into base6 number
- decimalToBase6(dec);
- }
- int main()
- {
- // Calling the function and passing a binary number with each function call
- binaryToDecimal(11011011);
- binaryToDecimal(10110111);
- binaryToDecimal(10110111);
- binaryToDecimal(11011011);
- return 0;
- }
Sample Output:
The Base6 equivalent of 11011011 = 1003 The Base6 equivalent of 10110111 = 503 The Base6 equivalent of 10110111 = 503 The Base6 equivalent of 11011011 = 1003
3. C Implementation
- #include <stdio.h>
- // Function To convert a decimal number into its equivalent number in base 6
- void decimalToBase6(int dec)
- {
- // Output variable will store the result
- int output = 0;
- int pv = 1;
- // The while loop terminates when the dec number becomes equal to zero
- while (dec > 0)
- {
- output = output + pv * (dec % 6);
- dec = dec / 6;
- pv = pv * 10;
- }
- printf("%d\n", output);
- }
- // Function to convert a binary number into its equivalent decimal number
- void binaryToDecimal(int n)
- {
- // The dec variable will store the decimal number
- int dec = 0;
- int pv = 1;
- int temp = n;
- // The while loop terminates when the temp (Binary number) becomes equal to zero
- while (temp > 0)
- {
- dec = dec + pv * (temp % 10);
- temp = temp / 10;
- pv = 2 * pv;
- }
- printf("The Base6 equivalent of %d = ", n);
- // Calling the function to convert the dec into base6 number
- decimalToBase6(dec);
- }
- int main()
- {
- // Calling the function and passing a binary number with each function call
- binaryToDecimal(101010);
- binaryToDecimal(111101);
- binaryToDecimal(101101);
- binaryToDecimal(111010);
- return 0;
- }
Sample Output:
The Base6 equivalent of 101010 = 110 The Base6 equivalent of 111101 = 141 The Base6 equivalent of 101101 = 113 The Base6 equivalent of 111010 = 134
4. Python Implementation
- # Function To convert a decimal number into its equivalent number in base 6
- def decToBase6(dec):
- pv = 1
- output = 0 # Output variable will store the base6 equivalent of the given binary number
- # The while loop terminates when the dec number becomes equal to zero
- while(dec > 0):
- output = output + pv*(dec % 6)
- pv = pv * 10
- dec = dec // 6 # Integer Division
- print(output)
- # Function to convert a binary number into its equivalent decimal number
- def binaryToDecimal(n):
- # dec variable will store the decimal equivalent of the given binary number
- dec = 0
- pv = 1
- temp = n
- # The while loop terminates when the temp (Binary number) becomes equal to zero
- while(temp > 0):
- dec = dec + pv*(temp % 10)
- pv = pv * 2
- temp = temp//10 # Integer Division
- print(f"The base6 equivalent of {n} = ", end="")
- decToBase6(dec)
- # Calling the function and passing a binary number with each function call
- binaryToDecimal(1010101)
- binaryToDecimal(1111010)
- binaryToDecimal(1011011)
- binaryToDecimal(1110100)
Sample Output:
The base6 equivalent of 1010101 = 221 The base6 equivalent of 1111010 = 322 The base6 equivalent of 1011011 = 231 The base6 equivalent of 1110100 = 312