Infinitely large constant INF

Source: Internet
Author: User
Tags constant
#define INF 0x3f3f3f3f
0x3f3f3f3f decimal is 1061109567, that is, 10^9 level (and 0x7fffffff (32-bit int maximum) an order of magnitude), and the general situation of the data are less than 10^9, So it can be used as infinity without the case that the data is larger than infinity.
On the other hand, since the general data is not greater than 10^9, when we add infinity to a data, it does not overflow (this satisfies the "infinity plus a poor number is still infinite"), in fact 0x3f3f3f3f+0x3f3f3f3f= 2122219134, this is very large but not more than the 32-bit int range, so 0x3f3f3f3f also meet our "Infinity plus infinity or infinity" demand.
The biggest benefit: if we want to clear an array, we usually use memset (A,0,sizeof (a)), but when we want to assign an array to infinity (such as the initialization of the adjacency matrix when solving the graph theory problem), you cannot use the Memset function.      Because Memset is in bytes, it is able to clear the array 0 because 0 of each byte is 0, now good, if we set infinity as 0x3f3f3f3f, then the miracle happens, 0x3f3f3f3f each byte is 0x3f. So to put an integer array all to infinity, we only need memset (A,inf,sizeof (a))

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: 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.