Ispoweroftwo determines the power of 2, and ispoweroftwo determines

Source: Internet
Author: User
Tags decimal to binary

Ispoweroftwo determines the power of 2, and ispoweroftwo determines

The first result is:

Public bool IsPowerOfTwo (int n ){
If (n <1) return false; // The power of 2 must be greater than 0
Return (n & (n-1) = 0 );
}

Analysis: The power of 2 can be calculated using the Left shift (<) in the computer. To understand the functions of n & (n-1), see the following:

N & (n-1): Change the bitwise of the binary representation of n to 0. Let's look at a simple example:
N = 10100 (Binary), then (n-1) = 10011 = "n & (n-1) = 10000
We can see that the first digit is 0.
After understanding the role of n & (n-1), what applications does it have?
1. Calculate the number of 1 in the binary representation of a certain number
While (n> 0 ){
Count ++;
N & = (n-1 );
}

2. Determine whether a number is a power of 2.
N> 0 & (n-1) = 0)

3. calculate N! The number of prime factor 2.
N! Number of prime factor 2 = [N/2] + [N/4] + [N/8] + ....
The following is a simple example to deduce the process: N = 10101 (binary representation)
Now we track the 1 of the highest bits without considering that the other bits are 0,
In
[N/2] 01000
[N/4] 00100
[N/8] 00010
[N/8] 00001
The sum of all values is 01111 = 10000-1.
We recommend that you use the following code: (10101 )! The number of prime factor 2 is 10000-1 + 00100-1 + 00001-1 = 10101-3 (Binary represents the number of 1)

Generally, N! The number of prime factor 2 is N-(N binary represents the number of 1 in)

Currently, we can only see these applications, but as long as we understand the principles and functions of n & (n-1), it will be easier to solve related problems.

Original article: http://6460646.blog.163.com/blog/static/27779875201132893614412/

 

Int n = 16; // It must be greater than 0

The conversion of integers in C # is as follows:
Var x = 2;
Console. writeLine ("n" + x + "hexadecimal:" + Convert. toString (n, x) + "; n-1 binary:" + Convert. toString (n-1, x ));
X = 8;
Console. writeLine ("n" + x + "hexadecimal:" + Convert. toString (n, x) + "; n-1 binary:" + Convert. toString (n-1, x ));
X = 10;
Console. writeLine ("n" + x + "hexadecimal:" + Convert. toString (n, x) + "; n-1 binary:" + Convert. toString (n-1, x ));
X = 16;
Console. writeLine ("n" + x + "hexadecimal:" + Convert. toString (n, x) + "; n-1 binary:" + Convert. toString (n-1, x ));

Var B = n (n-1 );

Console. WriteLine (B );

Ps: & operator you can chat with me or leave a message if you are not clear about it

The manual calculation of the conversion between binary and decimal is as follows:

Conversion to binary mainly includes the following types: positive integer to binary, negative integer to binary, decimal to binary;

1. convert a positive integer to binary. The key points must be remembered: Except for the two, the remainder is sorted in reverse order, and the high position is supplemented by zero.

Divide 2.42 by 2 and the remainder is 010101 respectively. Then we sort them backwards. The binary value of 42 is the more intuitive expression shown in 101010.2.

3. The number of bytes in the computer is fixed, such as 8-bit, 16-bit, or 32-bit. Therefore, when the number of digits is not enough, the high position is zero. As said, as shown in 3, 42 is converted to binary. 00101010, that is, the standard format is (42) 10 = (00101010) 2. Remember it.

4.2. Convert negative integers to binary

Method: First, convert the corresponding positive integer to a binary value, reverse the binary value, and then add one to the result. Take 42 as an example. The negative integer is interpreted as-42. The last is: (-42) 10 = (11010110) 2.

5. convert decimal points to binary values: multiply the number after the decimal point by 2. There is a result. Take the integer part of the result (either 1 or 0 ), then multiply the decimal part by 2, and then take the integer part of the result ...... And so on, until the decimal part is 0 or the number of digits is enough. Then, we can sort the integers in order to form a sequence of binary decimal parts, as shown in 0.125 and 5.

6. How should I convert the integer part of a decimal number to an integer greater than 0? If the preceding integer is converted to binary, the decimal number is converted to binary, and then the result is OK, as shown in 6.

7.

Integer binary conversion to decimal: first, the binary number is filled with digits. If the first digit is 0, it indicates a positive integer. If the first digit is 1, it indicates a negative integer.

First, let's look at the positive integer whose first place is 0. After completing the digits, multiply the values of the two digits respectively and then add them to decimal. For example, 1010 is converted to decimal, method 7.

8. if the first digit of the binary complement is 1, the first digit needs to be reversed before conversion. For example, if the first digit is 11101011 and the first digit is 1, the first digit should be reversed:-00010100, then, calculate the decimal value of 10100 as 20, so the decimal value is-20, as shown in Method 8.

9. to convert a decimal binary into a decimal number: for example, to convert 0.1101 to a decimal number, place the four digits in the binary number below (9) the value after the corresponding values are multiplied is the converted decimal.

 

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.