You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to operate NAND only on the last `n` bits of the numbers, you can use the following code:
28
+
29
+
```
30
+
n = 4
31
+
a = 0b1101
32
+
b = 0b1001
33
+
34
+
print bin((a NAND b) BAND ((1 lshift n ) - 1))
35
+
36
+
' Output 110
37
+
```
38
+
39
+
### Example 3: Two's complement
40
+
41
+
> Two's complement is a mathematical operation to reversibly convert
42
+
> a positive binary number into a negative binary number with equivalent negative value. ([Wikipedia](https://en.wikipedia.org/wiki/Two%27s_complement))
43
+
44
+
```
10
45
' Two's complement is the standard way of representing negative integers in binary.
11
46
' The sign is changed by inverting all of the bits and adding one.
12
-
Def invsgn(n) = ((n Nand n) + 1) - Frac(n) ' invert the sign of n
47
+
48
+
Def invsgn(n) = ((n Nand n) + 1) - Frac(n) ' invert the sign of n
49
+
13
50
While True Do
14
51
Input "Enter a number (Enter empty to stop) : ", n
15
52
If Isstring(n) Then
@@ -19,7 +56,7 @@ While True Do
19
56
Print "This is the number with inverted sign: "; invsgn(n)
0 commit comments