A conversion program is used to store data in a computer.

Source: Internet
Author: User

When I had nothing to do, I found a program design question to practice: Write an application and read an integer (that is, a binary integer) that only contains 0 and 1 ), then, the following decimal integer is printed. The modulo and division operators are required. So I wrote the following two methods:

Code:
  1. Public static long readbinarynum ()
  2. {
  3. Repeated scan = new partition (system. In );
  4. String binarynumstr = "";
  5. Boolean available = false;
  6. Do {
  7. System. Out. Print ("enter a binary INTEGER :");
  8. Binarynumstr = scan. nextline ();
  9. Available = pattern. Matches ("[0-1] +", binarynumstr );
  10. If (! Available)
  11. {
  12. System. Out. println ("non-binary data, this input is invalid! ");
  13. Continue;
  14. }
  15. If (binarynumstr. Length ()> = 19)
  16. {
  17. System. Out. println ("the data is too big. This input is invalid! ");
  18. Available = false;
  19. Continue;
  20. }
  21. } While (! Available );
  22. Return long. parselong (binarynumstr );
  23. }
  24. /**
  25. * Convert the binary value to the corresponding decimal value.
  26. * @ Param binarynum the binary value to be converted
  27. * @ Return refers to the corresponding decimal value.
  28. */
  29. Public static long binarynum2decimalnum (long binarynum)
  30. {
  31. System. Out. println (binarynum );
  32. Long decimalnum = 0;
  33. Int position [] = new int [(INT) math. log10 (binarynum) + 1];
  34. Position [0] = (INT) (binarynum/Math. Pow (10, position. Length-1 ));
  35. For (INT I = 1; I <position. length; I ++)
  36. {
  37. Position [I] = (INT) (binarynum % (math. Pow (10, position. Length-I ))
  38. /(Math. Pow (10, position. Length-I-1 )));
  39. }
  40. For (INT I = 0; I <position. length; I ++)
  41. {
  42. Decimalnum + = position [position. length-i-1] * Math. Pow (2, I );
  43. System. Out. Print (position [I] + "");
  44. }
  45. System. Out. println ();
  46. Return decimalnum;
  47. }

During the test, I found that if the input data is 11111111111111111 (that is, 17 1), the program will encounter a problem during conversion, the result is 131072 (and the correct result should be 131071), so I tested it again. The final problem is found in the remainder operation:

Code:
  1. Long n = 111111111111111l; // 17 1
  2. System. Out. println (N % math. Pow (10, 1 ));
  3. System. Out. println (N %10.0 );
  4. System. Out. println (N % 10 );

The program runs and the output result is:

Code:
  1. 2.0
  2. 2.0
  3. 1

When I see this, I think this should be related to the storage structure of the numeric value in the computer or the division operation mechanism, but I am not quite sure what is going on here! So I wrote this article to keep you updated. I hope you will discuss it and give me some advice!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.