// Single variable:
template<typename T>
voidprint(T value) {
std::cout << value << "";
}
// Multiple/Any number of arguments:
template<typename ...T>
voidprint(T&&... args) {
((std::cout << args << ""), ...);
}
General
Bit manipulation
& over % for parity checks - n & 1 would evaluate to be true (a boolean to be used inside an if-conditional over the ordinary n % 2 != 0) if n is odd (given that the least significant bit is always set if the number is not even).
<</>> for mul/div operations with the powers of two. Ideal for conversions in between the units of information:
print("\n", ((1 << 10) << 10), "Kilobytes make up", (1048576 >> 20), "Gigabyte.");
// 1048576 Kilobytes make up 1 Gigabyte.
Mess with a particular bit:
n |= 1 << bitNum; // Set
n &= ~(1 << bitNum); // Clear
n ^= 1 << bitNum; // Toggle, much like https://stackoverflow.com/a/60946658/11422223
RGB breakdown: (conversions to and from Hex or RGBA possible as well)
// Considering red-green-blue to be represented by three sets of 8 bits, from left to right or from the MSB to the LSB:
int blue = rgb & 0xFF0000, green = (rgb >> 8) & 0xFF00, red = (rgb >> 16) & 0xFF;
rgbAgain = (red << 16) + (green << 8) + blue;
Given that the difference between the lowercase and uppercase versions of ASCII alphabets in integers is 32, a quick switch to those would be to respectively set or clear the sixth bit (since 26 - 1 = 32, with the rightmost bit being 20) from the right using appropriate characters:
print((char)('a' & '_'), (char)('a' | '')); // A a// As expected, toggle works as well:
std::cout << (char)('X' ^ '') << (char)('d' ^ ''); // xD
Check for a number to be a power of 2:
bool powerOfTwo = n && !(n & (n - 1));
if(powerOfTwo) print(n, "tends to be a power of 2.");