Tuesday, 15 March 2016

15th March

Bitmasks - use when you want to save some space when using variables
Can use a field of bits to pass many arguments to a function without the need for a struct
Can also save parameter space, imagine a function which has to take 20 different booleans, you'd have to pass many true/false declarations
Instead just take a single int and then call the function with the options enabled
Can return something like status&aliveFlag - when using &, both things must be true

Vector creates a container of bits - it is not standard (sometimes causes issues)
for (auto& v : bools) is an easy way to iterate through a vector, checking if true
std::bitset<sizeOfBitset> bitsetName (hex value for which bit you want enabled)
can use .set, .flip, .reset with a bitset

Custom allocators
Deleting an already deleted object breaks the program, so making a custom deleter can be a good way to circumvent that problem
Customised new and delete functions, if done properly, will almost always be as fast as or faster than the default new and delete functions
Can lead to a significant speed up!!!!!

Compiler automatically adds padding to the size of variables (bit size) to make sure that each new variable starts on an even number in its memory location

To summarise, bitmasks are good to save space and make code more legible. Custom made functions and such to overwrite stock compiler ones are usually not worth the effort especially for amateur programmers. Automatic padding can be helpful but when it isn't, it can be manipulated

No comments:

Post a Comment