From 90a59cd39bde3fac3afae43b5f99029df484a655 Mon Sep 17 00:00:00 2001 From: Thomas Braun Date: Thu, 19 Oct 2023 21:09:20 +0200 Subject: [PATCH] FindRightMostHighBit: Add it --- Packages/MIES/MIES_Utilities.ipf | 22 ++++++++++++++++++++++ Packages/tests/Basic/UTF_Utils.ipf | 11 +++++++++++ 2 files changed, 33 insertions(+) diff --git a/Packages/MIES/MIES_Utilities.ipf b/Packages/MIES/MIES_Utilities.ipf index 19e0e1c08b..3905e7dd21 100644 --- a/Packages/MIES/MIES_Utilities.ipf +++ b/Packages/MIES/MIES_Utilities.ipf @@ -6839,3 +6839,25 @@ threadsafe Function/S UpperCaseFirstChar(string str) return UpperStr(str[0]) + str[1, len - 1] End + +/// @brief Find the right most high bit +/// +/// @param value integer value in the range [0, 2^64] +/// +/// @return right most high bit or NaN in case nothing could be found +threadsafe Function FindRightMostHighBit(uint64 value) + + variable i + uint64 bit + + for(i = 0; i < 64; i += 1) + + bit = value & (1 << i) + + if(bit) + return i + endif + endfor + + return NaN +End diff --git a/Packages/tests/Basic/UTF_Utils.ipf b/Packages/tests/Basic/UTF_Utils.ipf index b5ccde41a9..983821513a 100644 --- a/Packages/tests/Basic/UTF_Utils.ipf +++ b/Packages/tests/Basic/UTF_Utils.ipf @@ -7316,3 +7316,14 @@ Function TestSearchWordInString() CHECK_EQUAL_STR(prefix, "ab#") CHECK_EQUAL_STR(suffix, "?efgh") End + +static Function TestFindRightMostHighBit() + + Make/FREE/N=(64) result = FindRightMostHighBit(1 << p) == p + CHECK_EQUAL_VAR(sum(result), 64) + + CHECK_EQUAL_VAR(FindRightMostHighBit(0), NaN) + + CHECK_EQUAL_VAR(FindRightMostHighBit(3), 0) + CHECK_EQUAL_VAR(FindRightMostHighBit(18), 1) +End