aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-11-21 21:05:59 +0000
committerGitHub <noreply@github.com>2022-11-21 21:05:59 +0000
commitc6026ce36cd7a73d4851bfcf09f831b8b671b95c (patch)
treeb6f07d237192675340d51f77d2c50ed27a55eeb3
parentc9b9e079f49982e2b4106c0dcd552b4fdbe92d90 (diff)
downloadperlweeklychallenge-club-c6026ce36cd7a73d4851bfcf09f831b8b671b95c.tar.gz
perlweeklychallenge-club-c6026ce36cd7a73d4851bfcf09f831b8b671b95c.tar.bz2
perlweeklychallenge-club-c6026ce36cd7a73d4851bfcf09f831b8b671b95c.zip
Update README.md
-rw-r--r--challenge-192/james-smith/README.md15
1 files changed, 15 insertions, 0 deletions
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