Added Tools class

A class that provides some static functions
This commit is contained in:
Jonny007-MKD 2014-01-06 20:27:40 +01:00
parent 116c420c7a
commit 0f1be15f30
2 changed files with 56 additions and 0 deletions

47
Hazard/Hazard/Tools.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "stdafx.h"
#include "Tools.h"
using namespace std;
// convert the binary representation of x to a string with the specified length
char* Tools::BinaryToChars(uint x, char length)
{
// warning: this breaks for numbers with more than 64 bits (= variables)
char* c = new char[length+1];
c += length; // last char
*c = 0;
do
{
*--c = '0' + (x & 1); // 0 or 1 at the specified position
x >>= 1;
} while (--length);
return c;
}
// converts a gray number to its binary representation
// yes, this is quite slow and we could generate a lookup table to speed it up.
uint Tools::GrayToBinary(uint x)
{
uint x1 = x;
char r = 0; // r = ceil(ld(x))
do
r++;
while (x1 >>= 1);
if ((r & (r-1)) != 0) // keine Potenz von 2
{
char r1 = 0; // r1 = ceil(ld(r))
do
r1++;
while (r >>= 1);
r = 1 << r1; // aufrunden auf Zweierpotenz
}
for (char i = 1; i < r; i++) // Umwandeln von Gray in Binary
x ^= x / 2;
return x;
}

9
Hazard/Hazard/Tools.h Normal file
View file

@ -0,0 +1,9 @@
#pragma once
class Tools
{
public:
static char* Tools::BinaryToChars(uint x, char length);
static uint GrayToBinary(uint x);
};