[GO] Converting from decimal to binary is too difficult to do without long division! !!

I learned binary conversion in high school mathematics

I attend the Faculty of Informatics at a national university, and recently I have come to use binary numbers frequently in information science experiments. In the experiment, the designed logic circuit was sent to something like a board like a board with integrated circuits called FPGA, and it was like a college student like an elementary school student like "I made 1 plus 1". I'm doing that. I don't know what it means, but signed binary numbers are interesting because they add positive numbers and positive numbers to make a negative number. However, I've been accustomed to decimal numbers for 20 years, and I basically think of things in decimal numbers. My brain miso is not Kamen Rider Zero One. (I don't know because I haven't seen the actual Kamen Rider) As expected, it is possible to convert decimal numbers ⇔ binary numbers by mental arithmetic up to about 5 to 6 digits, but if the number of digits exceeds that, use a calculator or perform manual calculation. In this article, I would like to introduce a decimal-to-binary conversion program that I made a long time ago when I was fishing for an old computer recently.

First of all, the program

This time I wrote it in java. When I was in the first year of college, I only touched on java, so I believed that there were only Japanese, English, and java in this world. It's a time when I'm not used to programming yet, so I think there are many things I would like to say about design and coding, but I hope you can see it with a warm eye as if you were watching the growth of your cute grandchildren. Paste it as it is without any modification.

binary_change.java


import java.util.Stack;
import java.util.*;

class binary_change{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);

    System.out.println("Input number.");
    int inputnum = sc.nextInt();

    transrate(inputnum);
  }

  public static void transrate(int num){
    final int numnumnum = num;
    Stack stack = new Stack();
    int stacksize = 0;
    stacksize = stack.size();
    int numnum = 1;
    do{
      numnum = numnum * 2;
      stacksize ++;
    }while(numnum < num);

    System.out.println("stacksize is " + stacksize);
    int nextnum1;
    int nextnum2;
    int surplus;
    int surplus1;
    int surplus2;

    surplus = num % 2;
    System.out.println("2 )" + num + "..." + surplus);
    System.out.println("----------");
    stack.push(surplus);
    do{
      nextnum1 = num / 2;
      surplus1 = num % 2;
      if (nextnum1 == 1) {
        break;
      }

      num = nextnum1;

      nextnum2 = num / 2;
      surplus2 = num % 2;


      System.out.println("2 )" + nextnum1 + "..." + surplus2);
      System.out.println("----------");
      stack.push(surplus2);
      if (nextnum2 == 1) {
        System.out.println("2 )" + nextnum2);
        stack.push(nextnum2);
        break;
      }

    }while (true);
    System.out.println("Deciminal number ---> Binary number");
    System.out.print(numnumnum + " ---> ");
    for (int i = 0; i < stacksize; i ++) {
      System.out.print(stack.pop());
    }
    System.out.println(" ");
  }
}

Even though I don't usually write java at all, I can understand this code or poop after 3 years of growth. But when I think about it carefully, I think that I used to write such a tool-type program in Gamshala. I think this kind of accumulation is really important.

Now, let's try to see how it behaves.

takeshitatakushuunoMacBook-ea:Desktop hirosugutakeshita$ java binary_change
Input number.
810
stacksize is 10
2 )810...0
----------
2 )405...1
----------
2 )202...0
----------
2 )101...1
----------
2 )50...0
----------
2 )25...1
----------
2 )12...0
----------
2 )6...0
----------
2 )3...1
----------
2 )1
Deciminal number ---> Binary number
810 ---> 1100101010 
takeshitatakushuunoMacBook-ea:Desktop hirosugutakeshita$ 

Program description

As you can see from the result, this guy calculates the conversion by hand even though it is a calculator. There are two methods, the main and the conversion part. Almost all calculations are done in transrate (). The main method just takes a value and gives an argument.

I will also briefly explain the contents of transrate (). I'm still looking at it, but it's really like handwriting. Divide by 2 and divide it into quotient and remainder, and stack the remainder on the stack. Save the quotient in a variable for the next calculation. After that, I output the quotient and the remainder in a nice place so that it looks like a long division, and continue until the last quotient becomes 1. The point was to read the number of remainders (0 or 1 this time) in the long division of binary conversion (as is the case with other base conversions). I think it was written in the math textbook. Therefore, I think it is one idea to select the stack for the data structure to be used. Since the values are output in the reverse order of stacking, it is the same as reading because the remainder of the long division was done.

スタック .png

Thinking

I don't think I call this an "algorithm", but I think it's pretty interesting because it converts to binary without using the conversion calculation library that probably exists if you look for it. Please use this tool when you are asked about 100 questions to convert to binary using long division.

I find it sometimes interesting to see old programs. As well as feeling my own growth, I also found that it would be difficult to read if I wrote it like this. Sometimes I thought it would be nice to do this. (Extremely loose)

Recommended Posts

Converting from decimal to binary is too difficult to do without long division! !!
Precautions when converting from decimal number to binary number