Indeed, I use bitwise operations for anything involving powers of 2.
alleypuppy: viewtopic.php?f=3&t=1429&p=5537#p5537I could also explain it as-is. It is based on adding bits together and seeing if the input string contains characters at the bits represented value. Counting down from 12 to 0 (13 iterations) can count a string up to about 8192 characters long. From 11 is 4096, from 10 is 2048 and so on, if for example you wanted to make the script milliseconds faster if you know an estimated max length of any strings you count.
1. Creates "binary" variable "len" with the value 4096.
2. Checks to see if the input string contains characters after position 4096.
3. If it doesn't, it removes the binary value 1 from the 12th position (value 4096) of the "binary" variable.
4. Repeats to check the next binary position (2048, continually lowering values).
What it accomplishes is, say you have a string 8035 characters long:
It counts 4096 + 2048 + 1024 + 512 + 256 + 64 + 32 + 2 + 1 to get 8035.
The process goes like... 4096 + 2048 + 1024 + 512 + 256
+ 128 - 128 + 64 + 32
+ 16 -16 + 8 - 8 + 4 - 4 + 2 + 1. Red numbers cause the value to go beyond the length of the string, so they are corrected.
The reason this works using only powers of 2 instead of say units of 10, 100, 1000 etc is because the nature of powers of 2 is you can divide them repeatedly all the way down to the number 1 as whole integers. Also, dividing the number by 2 is the quickest logical way. If you know the number game "higher or lower" where you have to guess say a number between 1 and 100, it's quickest to go like...
50 (halfway 1 and 100)
higher
75 (halfway 50 and 100)
higher
87 (*halfway 75 and 100)
lower
82 (*halfway 75 and 87)
higher
85 (*halfway 82 and 87)
lower
83 (*halfway 82 and 85)
bingo
*roughly, see this is why you can't do units of 10