Calculate the N power of 2

Source: Internet
Author: User

The for loop class can be used to accumulate the power of 2, but its length is limited. The power of calculation is used to review the length of the Data Type again.

 

As follows:

Byte byte unsigned integers 8 0 to 255
Sbyte signed integer 8-128 to 127
Int int32 signed integers 32-2,147,483,648 to 2,147,483,647
Uint uint32 unsigned integer 32 0 to 4294967295
Short int16 signed integer: 16-32,768 to 32,767
Ushort uint16 unsigned integer 16 0 to 65535
Long int64 signed integer 64-922337203685477508 to 922337203685477507
Ulong uint64 unsigned integer 64 0 to 18446744073709551615
Float single precision float 32-3.402823e38 to 3.402823e38
Double double float 64-1.79769313486232e308 to 1.79769313486232e308
The Unicode character used in the single UNICODE character 16 text of char
Boolean logical Boolean Value Type 8 true or false
All other types of base types of Object
String string Character Sequence
Decimal decimal exact decimal type or Integer type. It can represent the decimal number with 29 valid digits: 128 ± 1. 0 × 10E −28 to ± 7. 9 × 10e28

When we find that the power of 2 is 100, we first observe that double is 64 bits, no, and the length is not enough. Decimal!

Private Static void getpow (INT num)

Decimal sum = 0;
For (INT I = 1; I <num; I ++)
{
Decimal temp = 2;
For (INT n = 1; n <I; n ++)
{
Temp = temp * 2;
}
Sum + = temp;
Console. writeline ("for_sum ------" + I + "-------" + sum); // The decimal value range is 128 bits. 32 High, 32 middle, 32 low, the low occupies 32 digits, so only the power of 2 can be calculated. However, it is not clear that decimal can be used to calculate 95 bits.
}

Console. writeline (string. format ("{0}/n {1}", math. pow (2, num), sum); // This method can be used for simple implementation, but many digits are skipped, not completely displayed, not desired.

 

// The following method can solve the problem of N power calculation.

Int [] iarr = new int [num];
Int J = 0;
For (INT I = 1; I <iarr. Length + 1; I ++)
{
Iarr [I-1] = convert. toint32 (math. floor (math. pow (2, iarr. length) % math. pow (10, I)/math. pow (10, (I-1); // obtain the single-digit method in the power of N, and put it in the array, starting from 1.
}
For (INT I = iarr. Length-1; I> = 0; I --)
{
If (iarr [I]! = 0)
{
J = I;
Break;
}
Else
Continue;
}
For (INT I = J; I> = 0; I --) // display all digits in reverse mode to obtain the power of n times.
{
Console. writeline (string. Format ("{0} ------ {1}", iarr [I], I ));
}

Console. writeline ();
Console. readkey ();

}

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.