From 0f1be15f30e18419e5f334695aea22a5dd6cece4 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD <1-23-4-5@web.de> Date: Mon, 6 Jan 2014 20:27:40 +0100 Subject: [PATCH] Added Tools class A class that provides some static functions --- Hazard/Hazard/Tools.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ Hazard/Hazard/Tools.h | 9 ++++++++ 2 files changed, 56 insertions(+) create mode 100644 Hazard/Hazard/Tools.cpp create mode 100644 Hazard/Hazard/Tools.h diff --git a/Hazard/Hazard/Tools.cpp b/Hazard/Hazard/Tools.cpp new file mode 100644 index 0000000..42beb6e --- /dev/null +++ b/Hazard/Hazard/Tools.cpp @@ -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; +} \ No newline at end of file diff --git a/Hazard/Hazard/Tools.h b/Hazard/Hazard/Tools.h new file mode 100644 index 0000000..e4ebca0 --- /dev/null +++ b/Hazard/Hazard/Tools.h @@ -0,0 +1,9 @@ +#pragma once + +class Tools +{ +public: + static char* Tools::BinaryToChars(uint x, char length); + + static uint GrayToBinary(uint x); +}; \ No newline at end of file