Skip to content

Commit

Permalink
FindRightMostHighBit: Add it
Browse files Browse the repository at this point in the history
  • Loading branch information
t-b committed Oct 19, 2023
1 parent 3126726 commit e0fcb1c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Packages/MIES/MIES_Utilities.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -6710,3 +6710,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
11 changes: 11 additions & 0 deletions Packages/tests/Basic/UTF_Utils.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -7253,3 +7253,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

0 comments on commit e0fcb1c

Please sign in to comment.