diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index c67e050..bc5a438 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -84,6 +84,9 @@ namespace MUtils MUTILS_API quint32 next_rand32(void); MUTILS_API quint64 next_rand64(void); + //Parity + MUTILS_API bool parity(quint32 value); + //Remove File/Dir MUTILS_API bool remove_file(const QString &fileName); MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive); diff --git a/src/Global.cpp b/src/Global.cpp index 684bcc2..42d2189 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -109,6 +109,23 @@ QString MUtils::rand_str(const bool &bLong) return QString("%1%2").arg(rand_str(false), rand_str(false)); } +/////////////////////////////////////////////////////////////////////////////// +// COMPUTE PARITY +/////////////////////////////////////////////////////////////////////////////// + +/* + * Compute parity in parallel + * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel + */ +bool MUtils::parity(quint32 value) +{ + value ^= value >> 16; + value ^= value >> 8; + value ^= value >> 4; + value &= 0xf; + return ((0x6996 >> value) & 1) != 0; +} + /////////////////////////////////////////////////////////////////////////////// // TEMP FOLDER ///////////////////////////////////////////////////////////////////////////////