Go maximum number of unsigned

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

here is actually mainly translated http://blog.golang.org/constants, which An exercise:the largest unsigned int section.

how to construct the largest unsigned number. For an unsigned number that determines the number of bits, as long as the shift is possible, for example: for UInt32, as long as const MAXUINT32 = 1<<32-1.

However, for a uint with an indeterminate number of bits, this is not the case, in fact, we know that the largest number of unsigned and 1 bits is the same. However, either the const maxuint UINT =-1//Error:negative value , or the const maxuint UINT = UINT ( -1)//Er Ror:negative value
, can not be compiled, the reason is naturally the type and scope of the conflict. But at runtime, the value of 1 can indeed be passed to unsigned yes, how to compile and pass this value to the unsigned number? OK

var u uint

var v =-1

U = UINT (v)

So it's done. Another method is to reverse 0. The difference is that after 0 inverse, is infinitely more than 1, then a finite range of uint to pick it up, obviously will be the compiler think of the loss of precision. The method is to construct a definite number of bits 0, and then reverse, as follows:

Const MAXUINT = ^uint (0)

Here are two ways to get the maximum number of unsigned numbers.

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.