Exercise 2-4
authorwatkinsr <ryanwatkins54@gmail.com>
Fri, 5 Jun 2020 15:13:43 +0000 (16:13 +0100)
committerwatkinsr <ryanwatkins54@gmail.com>
Fri, 5 Jun 2020 15:13:43 +0000 (16:13 +0100)
goproglang
main.go
popcount/popcount.go

index 625fe71a7712e80677d9f27d831fba9d29b9a875..36005ae90c751a5269cf4dd9b1bbb52c6a9ebe5c 100755 (executable)
Binary files a/goproglang and b/goproglang differ
diff --git a/main.go b/main.go
index 53b41eda4594e08941b978b6934e8df76e49682a..d7d11a6491103393f3e485c247bf4d79cb6ab464 100644 (file)
--- a/main.go
+++ b/main.go
@@ -4,24 +4,30 @@ import (
   // "git.ryanwatkins.me/goproglang/httpserve"
   "time"
   "fmt"
-  "git.ryanwatkins.me/goproglang/tempconv"
+  // "git.ryanwatkins.me/goproglang/tempconv"
   "git.ryanwatkins.me/goproglang/popcount"
 )
 
 func main() {
   // httpserve.Serve()
-  fmt.Println(tempconv.CToF(tempconv.BoilingC))
-  fmt.Println(tempconv.CToK(tempconv.AbsoluteZeroC))
-  fmt.Println(tempconv.FToK(tempconv.CToF(tempconv.FreezingC)))
+  // fmt.Println(tempconv.CToF(tempconv.BoilingC))
+  // fmt.Println(tempconv.CToK(tempconv.AbsoluteZeroC))
+  // fmt.Println(tempconv.FToK(tempconv.CToF(tempconv.FreezingC)))
 
   // Exercise 2-3
   t := time.Now()
-  fmt.Printf("popcount: %d\n", popcount.PopCount2(120))
+  amount := uint64(1023)
+  popcount.PopCount2(amount)
   res := time.Since(t)
   fmt.Printf("Time using single statement: %v\n", res)
   
   t = time.Now()
-  fmt.Printf("popcount: %d\n", popcount.PopCount(120))
+  popcount.PopCount(amount)
   res = time.Since(t)
   fmt.Printf("Time using loop: %v\n", res)
+  
+  t = time.Now()
+  v := popcount.PopCountShiftValue(amount)
+  res = time.Since(t)
+  fmt.Printf("Time: PopCountShiftValue: %v -> %d\n", res, v)
 }
index 5202d93350f0865f912d719ee25378cb15b5bbe7..fc7a7a990a7d706cd99af12e01865f49d195cac2 100644 (file)
@@ -1,5 +1,21 @@
 package popcount
 
+// import (
+//   "fmt"
+// )
+
+func PopCountShiftValue(x uint64) int {
+  count := 0
+  mask := uint64(1)
+  for i := 0; i < 64 && x > 0; i++ {
+    if x&mask > 0 {
+      count++
+    }
+    x >>= 1
+  }
+  return count
+}
+
 // pc[i] is the population count of i
 var pc [256]byte