Skip to content

Commit 240ee4b

Browse files
authored
Update 676-language-xor.markdown
1 parent 891e6da commit 240ee4b

File tree

1 file changed

+14
-40
lines changed

1 file changed

+14
-40
lines changed
Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
11
# XOR
22

3-
> a XOR b
3+
> n = a XOR b
44
5-
Bitwise exclusive OR. Equivalent syntax to: a ~ b
5+
Bitwise exclusive OR.
66

7+
Truth table:
78

8-
~~~
9+
| a | b | a XOR b |
10+
|:-:|:-:|:-------:|
11+
| 0 | 0 | 0 |
12+
| 0 | 1 | 1 |
13+
| 1 | 0 | 1 |
14+
| 1 | 1 | 0 |
915

10-
' return true if n is ASCII code of letter (A-Z|a-z); e.g. ? isAbc(Asc("z"))
11-
Def isAbc(n) = ((n >= 65 And n <= 90) Or (n >= 97 And n <= 122))
12-
13-
' change case of string s; mode is -1, 0, 1 (lower, invert, upper).
14-
Func ccase(s, mode)
15-
Local i, n
16-
17-
For i = 1 To Len(s)
18-
n = Asc(Mid(s, i, 1))
19-
If isAbc(n) Then
20-
Select Case mode
21-
Case -1: n = n Bor 0b00100000 ' set bit-5 to lower case
22-
Case 0: n = n Xor 0b00100000 ' invert bit-5 to invert case
23-
Case 1: n = n Band 0b11011111 ' reset bit-5 to upper case
24-
End Select
25-
s = Replace(s, i, Chr(n))
26-
Endif
27-
Next
28-
ccase = s
29-
End
30-
31-
' run demo:
32-
Repeat
33-
Cls
34-
Input "Enter a string (Enter empty to stop): ", s
35-
36-
Print
37-
Print " Upper case: "; ccase(s, 1)
38-
Print " Lower case: "; ccase(s, -1)
39-
Print " Invert case: "; ccase(s, 0)
40-
Pause
41-
Until s = ""
42-
43-
~~~
44-
45-
XOR is Not equivalent syntax to: a ~ b
46-
~ is bitwise NOT, for example: ~b
16+
### Example
4717

18+
```
19+
print 0 xor 1 ' Output: 1
20+
print bin(0b1100 xor 0b1010) ' Output: 110
21+
```

0 commit comments

Comments
 (0)