C++ Boost.Integer














































C++ Boost.Integer




Boost is a set of libraries for Advanced C++ Programming. It is used by coders to perform various tasks like multithreading,
image processing, regular expressions, unit testing etc using the inbuilt source libraries. It is used for competitive
programming and meta programming.

 

We will study specifically about Boost.Integer here!

 

Boost.Integer allows us to work withvery large numbers. For example : 2128  bits. The use of using integer data types under 

Boost library, increases the accuracy and precision of operations. It also reduces the chances of error.

 

    • Header file : <boost/cstdint.hpp>
    • Script : ECMA
    • Requirement : C++11
    • Compiler : Codeblocks 17.12



The following are the supported integer data types under the namespace boost :

 

    • 1.    Exact-width Integer types


Declaration of signed integer: int#_t 

Declaration of unsigned integer: uint#_t 

  where # refers to the bits

 

   It defines an integer with exact # bits.

           

   Example:If we declare int8_t, it is capable of holding 8bits.

           

   The platform supports #= 8,16,32,64,128,256 . . .

 

    • 2.    Minimum-width Integer types

 

Declaration of signed integer: int_least#_t 

Declaration of unsigned integer: uint_least#_t 

  where # refers to the bits

 

  It defines an integer with minimum # bits.

           

  Example: If we declare int_least8_t, it is capable of holding 8bits or more.

           

  The platform supports #= 8,16,32,64,128,256 . . .

 

    • 3. Fastest minimum-width Integer types


Declaration of signed integer: int_fast#_t 

Declaration of unsigned integer: uint_fast#_t 

  where # refers to the bits

 

It defines an integer with minimum # bits.

           

Example: If we declare int_fast8_t, it is capable of holding 8bits or more. Their actual size is set to a value that 

provides optimum performance.

           

The platform supports #= 8,16,32,64,128,256 . . .

 

    • 4. Greatest-width Integer types


Declaration of signed integer: intmax_t 

Declaration of unsigned integer: uintmax_t 

 

It defines an integer with maximum bits. These data typesare capable of representing any value.

           

Example: If we declare intmax_t, an signed integer is declared that it is capable of holding maximum bits best suited 

according to the situation.

 

            

All the above integer data types can be used in any program according to the types of operations to be performed.

 


The following example illustrates the use of Boost.integer data types :


CODE :


/*This program calculates the size of different integer data
types

under boost library  */

 

#include <boost/cstdint.hpp>

#include <iostream>

 

using namespace boost;

using namespace std;

 

int main()

{

  int8_t i8 = 1;

  cout << sizeof(i8) << '\n';

 

#ifndef BOOST_NO_INT64_T

  uint64_t ui64 = 10;

  cout << sizeof(ui64) << '\n';

#endif

 

  int_least8_t il8 = 10;

  cout << sizeof(il8) << '\n';

 

  uint_least32_t uil32 = 1;

  cout << sizeof(uil32) << '\n';

 

  int_fast8_t if8 = 1;

  cout << sizeof(if8) << '\n';

 

  uint_fast16_t uif16 = 10;

  cout << sizeof(uif16) << '\n';

 

  intmax_t imax = 10;

  cout << sizeof(imax) << '\n';

 

  cout << sizeof(UINT8_C(1)) << '\n';

 

#ifndef BOOST_NO_INT64_T

  std::cout << sizeof(INT64_C(1)) << '\n';

#endif

 

  return 0;

}



 OUTPUT: 





Comments