From c6026ce36cd7a73d4851bfcf09f831b8b671b95c Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 21 Nov 2022 21:05:59 +0000 Subject: Update README.md --- challenge-192/james-smith/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/challenge-192/james-smith/README.md b/challenge-192/james-smith/README.md index 7c47f95f78..e015cf6ddb 100644 --- a/challenge-192/james-smith/README.md +++ b/challenge-192/james-smith/README.md @@ -74,6 +74,21 @@ int c_flip(int n) { Now - when comparing this to the other two: The C version is 4.5 times faster than the string version OR 35x faster than the equivalent Perl version. +A further re-write of the C gives: + +```C +int c2(int n) { + int o = n; + int m = 1; + while(o>>=1) { + m<<=1; + m++; + } + return n^m; +} +``` + +Here we compute a mask of `1`s as long as the binary representation of the number so for `25` = `11001` we have a mask of `11111` and so doing a bitwize XOR operation gives us `00110` or `6`. Which is even faster as it only does the XOR (`^`) once. {approx 5 times faster than the regex version} # Task 2 - Equal Distribution -- cgit