tbitops.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. discard """
  2. output: ""
  3. """
  4. import strutils
  5. const x = [1'i32, -1, -10, 10, -10, 10, -20, 30, -40, 50, 7 shl 28, -(7 shl 28), 7 shl 28, -(7 shl 28)]
  6. const y = [-1'i32, 1, -10, -10, 10, 10, -20, -30, 40, 50, 1 shl 30, 1 shl 30, -(1 shl 30), -(1 shl 30)]
  7. const res_xor = block:
  8. var tmp: seq[int64]
  9. for i in 0 ..< x.len:
  10. tmp.add(int64(x[i] xor y[i]))
  11. tmp
  12. const res_and = block:
  13. var tmp: seq[int64]
  14. for i in 0 ..< x.len:
  15. tmp.add(int64(x[i] and y[i]))
  16. tmp
  17. const res_or = block:
  18. var tmp: seq[int64]
  19. for i in 0 ..< x.len:
  20. tmp.add(int64(x[i] or y[i]))
  21. tmp
  22. const res_not = block:
  23. var tmp: seq[int64]
  24. for i in 0 ..< x.len:
  25. tmp.add(not x[i])
  26. tmp
  27. let xx = x
  28. let yy = y
  29. for i in 0..<xx.len:
  30. let z_xor = int64(xx[i] xor yy[i])
  31. let z_and = int64(xx[i] and yy[i])
  32. let z_or = int64(xx[i] or yy[i])
  33. let z_not = int64(not xx[i])
  34. doAssert(z_xor == res_xor[i], $i & ": " & $res_xor[i] & " " & $z_xor)
  35. doAssert(z_and == res_and[i], $i & ": " & $res_and[i] & " " & $z_and)
  36. doAssert(z_or == res_or[i], $i & ": " & $res_or[i] & " " & $z_or)
  37. doAssert(z_not == res_not[i], $i & ": " & $res_not[i] & " " & $z_not)