Home

Advertisement

   Journal    Friends    Archive    User Info    Memories
 

Зажги свою жизнь!

Mar. 11th, 2009 04:01 pm C/C++ preprocessor macro magic for versioning

Недавно возникла задача слепить строку версии программы. Оказалось не очень тривиально:

// где-то генерируется версия
#define VERSION_MAJOR 2
#define VERSION_MINOR 0
#define VERSION_BUILD 1019

// надо в коде получить свою строчку с версией
#define _TOSTRING(x) #x
#define TOSTRING(x) _TOSTRING(x)
#define FULL_VERSION_STRING "Version: "TOSTRING(VERSION_MAJOR)"."TOSTRING(VERSION_MINOR)"."TOSTRING(VERSION_BUILD)

// результат: "Version: 2.0.1019"

Leave a comment

Feb. 19th, 2009 01:32 pm Очень быстрый rand()

Подсмотрено здесь, рвёт сишный rand() как по скорости (в разы!) так и по качеству.


#include <iostream>

class FastRandom
{
public:
// just generate good initial seeds
FastRandom() : high(0xDEADBEEF), low(high ^ 0x49616E42) {}
unsigned int Rand()
{
high = (high << 16) + (high >> 16);
high += low;
low += high;
return high;
}
private:
unsigned int low;
unsigned int high;
};

int main()
{
FastRandom r;
for (int i = 0; i < 100; ++i)
std::cout << r.Rand() << std::endl;
}

4 comments - Leave a comment

 

Advertisement