Java Primitive Types: int vs. Integer

Short answer: An int is a number; an Integer is a pointer that can reference an object that contains a number. Using Integer for arithmetic involves more CPU cycles and consumes more memory. An int is not an object and cannot passed to any method that requires objects (just like what you said about Generics).

C++ 128/256-bit fixed size integer types

The Boost library has data types as part of multiprecision library, for types ranging from 128 to 1024 bits. #include <boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; int128_t mySignedInt128 = -1; uint128_t myUnsignedInt128 = 2; int256_t mySignedInt256 = -3; uint256_t myUnsignedInt256 = 4; int512_t mySignedInt512 = -5; uint512_t myUnsignedInt512 = 6; int1024_t mySignedInt1024 = -7; uint1024_t myUnsignedInt1024 = … Read more

C# dynamic type gotcha

The fundamental principle of “dynamic” in C# is: at runtime do the type analysis of the expression as though the runtime type had been the compile time type. So let’s see what would happen if we actually did that: dynamic num0 = ((Program.Factory.Empty)container).Value; That program would fail because Empty is not accessible. dynamic will not … Read more

Exact decimal datatype for C++?

The Boost.Multiprecision library has a decimal based floating point template class called cpp_dec_float, for which you can specify any precision you want. #include <iostream> #include <iomanip> #include <boost/multiprecision/cpp_dec_float.hpp> int main() { namespace mp = boost::multiprecision; // here I’m using a predefined type that stores 100 digits, // but you can create custom types very easily … Read more

How is a bool represented in memory?

Standard states that bool values behave as integral types, yet it doesn’t specify their concrete representation in memory: “Values of type bool are either true or false. As described below, bool values behave as integral types. Values of type bool participate in integral promotions” ~ C++03 3.9.1 ยง6 “A synonym for integral type is integer … Read more

Switch passed type from template

Yes, it is…but it probably won’t work the way you expect. template < typename T > void foo() { if (is_same<T, SomeClass>::value) …; else if (is_same<T, SomeClass2>::value) …; } You can get is_same from std:: or boost:: depending on your desire/compiler. The former is only in C++0x. The problem comes with what is in …. … Read more