Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform binary conversion to make it faster and clearer

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to convert binary to make it faster and clearer". Interested friends might as well take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to make binary conversion faster and clearer".

Binary: binary is a method of counting. Decimal numbers are most commonly used in our lives, that is, decimal numbers, that is, decimal numbers, while in computers, data are stored in binary numbers, that is, binary digits. Common decimal and hexadecimal systems are also used.

1. Binary conversion

Conversion between decimal systems is most commonly used to convert decimal to other decimal systems, while decimal can also be used as an intermediary for conversion between other binary systems. Now we take the most commonly used decimal-to-binary and binary-to-decimal conversion as examples, and other binary conversions are the same:

(1) Decimal to binary

Decimal number: 35

Method: divide the decimal number continuously by 2 (several digits are divided by several), record the remainder after each division by 2, and finally write the remainder backwards. If less than 8 digits are filled with 0 on the far right, this is the final binary number.

Because binary is used to be represented by the power of 8, it needs to be filled with 0, and the final binary number of decimal 35 is 0010 0011.

(2) binary to decimal number

Binary number: 1000 1010

Method: from right to left, mark each binary bit with a sequence number starting at 0, multiply the value of each bit by the ordinal power of 2 (several digits), and add up the result of each bit to get the decimal representation of the binary number.

2. Java code to achieve binary conversion

(1) Decimal to binary

Public static void TentoTwo (long n) {long res = 0; int I = 1; while (n > 0) {res + = n% 2 * I; n / = 2; I * = 10;} System.out.println (res);}

(2) binary to decimal number

/ / binary-to-decimal number public static void TwotoTen (long n) {long res = 0; int I = 0; while (n > 0) {res + = n * mi (2pm I); n / = 10; iSum + } System.out.println (res);} / / realize the power of a, public static int mi (int a, int b) {if (b = = 0) {return 1;} else if (b = = 1) {return a } else {int c = a; for (int I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report