Author Topic: Xor Magic ^2  (Read 6796 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Xor Magic ^2
« Reply #15 on: April 12, 2021, 11:06:01 am »
@NOVARSEG not seeing the relationship to Xor in your last posts.
« Last Edit: April 12, 2021, 11:08:26 am by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Xor Magic ^2
« Reply #16 on: April 12, 2021, 11:08:41 am »
Could be another hijacked post!
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Xor Magic ^2
« Reply #17 on: April 12, 2021, 02:58:48 pm »
Yep the thread went off topic but did not want to start a new thread

 XOR and other logic operators combined with memput and memget allow full control of cursor position and color


The code was just a simplified version.  I think XOR can be used to turn a cursor on and off.
« Last Edit: April 12, 2021, 03:03:47 pm by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Xor Magic ^2
« Reply #18 on: April 12, 2021, 10:32:19 pm »
More Xor Magic, use Xor to find the missing number from a set of integers from 1 to n:
Code: QB64: [Select]
  1. _Title "Find missing number" 'b+ 2021-04-12
  2. ' given an array of integers for 1 to n find the missing integer
  3.  
  4. DefLng A-Z
  5. n = Int(Rnd * 100) + 2 ' pick a number n of integers
  6. Dim nums(1 To n) ' set an array of numbers
  7. For i = 1 To n: nums(i) = i: Next
  8. For i = n To 2 Step -1 ' shuffle  the array
  9.     Swap nums(Int(i * Rnd) + 1), nums(i)
  10.  
  11. 'remove one of the integers
  12. r = Int(n * Rnd) + 1
  13. removed = nums(r) 'save number
  14. nums(r) = 0
  15.  
  16. 'find missing number  one way to find the missing number
  17. For i = 1 To n
  18.     sum = sum + nums(i)
  19. missing = n * (n + 1) / 2 - sum 'subtract sum from the formula for sum of integers 1 to n = n*(n+1)/2
  20. Print "Missing number found by difference for formula for sum of 1 to n integers and sum of integers in array."
  21. Print missing, removed
  22.  
  23. ' now we shall find the missing number using Xor alone!
  24. missing = 0
  25. For i = 1 To n
  26.     missing = missing Xor i
  27.     missing = missing Xor nums(i)
  28. Print: Print "Xor found missing compared to removed was:"
  29. Print missing, removed
  30.  
  31.  

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Xor Magic ^2
« Reply #19 on: April 13, 2021, 01:34:00 am »
Can Alice and Bob find the missing number?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Xor Magic ^2
« Reply #20 on: April 13, 2021, 11:42:13 am »
And Bob and Alice can create a deck of 52 shuffled numbers using Xor (instead of =):
Code: QB64: [Select]
  1. _Title "Find duplicate number" 'b+ 2021-04-13
  2. ' lets create a deck of 52 shuffled numbers with Xor
  3.  
  4. DefLng A-Z 'every variable, array and function is long
  5.  
  6. ReDim nums(1 To 52) ' set an array of numbers
  7.  
  8. 'obviously the first pick won't duplicate
  9. nums(1) = RandomPicker1To52
  10. index = 2
  11.  
  12. pickAgain:
  13. pick = RandomPicker1To52
  14. For i = 1 To index - 1
  15.     If (nums(i) Xor pick) = 0 Then GoTo pickAgain 'it was a duplicate
  16. nums(index) = pick
  17. index = index + 1
  18. If index <= 52 GoTo pickAgain
  19.  
  20. 'display results
  21. For i = 1 To 52: Print nums(i);: Next  'shouldn't be any duplicates or missing numbers, 1 to 52
  22.  
  23. Function RandomPicker1To52
  24.     RandomPicker1To52 = Int(Rnd * 52) + 1
  25.  
  26.