(Byte) 300 the actual value is 44

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Java core volume 1 (Chinese) 42 pages: (byte) 300 actual value is 44
Next let's take a look at how to convert:


System. out. println (byte) 300); System. out. println (Integer. toBinaryString (300); // output 100101100 // Step 2 BigInteger I = new BigInteger ("00101100", 2); // byte only uses 8 digits to intercept the System with 8 digits lower. out. println (I. intValue (); // output 44

Step 2

BigInteger
Can it be easily converted between various hexadecimal formats? It turns out that writing so many methods is in vain. Only the input should be a string.

First, if java supports 0b110101010 to represent binary

System.out.print(0b110101010)
Yes, but it is too long.

Baidu java binary to decimal find: Click to open the link

Excerpt backup:


Convert java binary to decimal, convert decimal to binary

19:48:23 | category: Java club | report | font size subscription

Import java. math. BigInteger;
Public class Hello {public static void main (String args []) {// 10 hexadecimal-> 2 hexadecimal String a = "121 "; // input value BigInteger src = new BigInteger (a); // convert to the BigInteger type System. out. println (src. toString (2); // convert to binary and output // Binary-> 10 hexadecimal String B = "1111001 "; // input value BigInteger src1 = new BigInteger (B, 2); // convert it to the BigInteger type System. out. println (src1.toString (); // convert to the 10th hexadecimal notation and output the result/*. Therefore, note the following two points: 1. the default parameter String of BigInteger constructor BigInteger (String src) is a 10-digit BigInteger (String src, int x). x is the hexadecimal type of the first src parameter.
2. The toString method toString () converts a value into a string in decimal order by default. ToString (int x) converts a value to a string in hexadecimal notation of parameter x */}}
In addition, the algorithm for converting decimal to binary is as follows:
1. specify a number;
2. According to the principle of decimal Binary Conversion: divide this number by 2. If it is a singular number, it is 1, and if it is an even number, it is 0 until the last number is 1. So what we need to do is to divide the given number by 2. if the result is an odd number, r = 1; otherwise, r = 0; so the loop goes down until the number is greater than = 1.
3. Then, the r value is sent to an array. Finally, print the content in the array from the back.
Import java. util. collections;
Public class Hi {
Public static void main (String [] args) {nation SC = new Nation (System. in); System. out. println ("please refer to the positive number of the binary system for importing a ten-digit system"); int n = SC. nextInt (); int r; int I = 0; int [] a = new int [20]; do {if (n % 2 = 1) r = 1; elser = 0; a [I] = r; n/= 2; I ++;} while (n> 0); System. out. println ("Ten hexadecimal notation after binary:"); for (int j = I-1; j> = 0; j --) {System. out. print (a [j]) ;}}


In addition, binary conversion to decimal import java. util. *; public class Hello {public static void main (String [] args) {consumer SC = new consumer (System. in); System. out. print ("enter a binary number"); int s = SC. nextInt (); int sum = 0; int I = 0; while (s! = 0) {sum = (int) (sum + s % 10 * (Math. pow (2, I); s = s/10; I ++;} System. out. println ("converted to decimal:" + sum );}}

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.