File size: 4,105 Bytes
389d072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from visma.simplify.simplify import simplify
from visma.functions.constant import Constant
from visma.io.tokenize import tokenizer

# TODO: Test cases.
# TODO: Implement GUI/CLI.


def logicalAND(token1, token2):
    """Implements Bitwise AND
    Arguments:
        token1 -- {list} -- List of tokens of a constant number
        token2 -- {list} -- List of tokens of a constant number

    Returns:
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    """

    comments = []
    animations = []
    token1, _, _, _, _ = simplify(token1)
    token2, _, _, _, _ = simplify(token2)
    if isinstance(token1, Constant) and isinstance(token2, Constant):
        comments += [['Converting numbers to Binary Illustrations: ']]
        animations += [[]]
        binaryValue1 = token1.binary()
        binaryValue2 = token2.binary()
        comments += [[]]
        animations += [[tokenizer('a = ' + str(binaryValue1))]]
        comments += [[]]
        animations += [[tokenizer('b = ' + str(binaryValue2))]]
        comments += [['Doing AND operation for each of the consecutive bit']]
        animations += [[]]
        resultValue = token1.calculate() & token2.calculate()
        comments += [['Final result is']]
        animations += [[tokenizer('r = ' + str(resultValue))]]
        token_string = 'r = ' + str(resultValue)
        return token_string, animations, comments
    else:
        return '', [], []


def logicalOR(token1, token2):
    """Implements Bitwise OR
    Arguments:
        token1 -- {list} -- List of tokens of a constant number
        token2 -- {list} -- List of tokens of a constant number

    Returns:
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    """

    comments = []
    animations = []
    token1, _, _, _, _ = simplify(token1)
    token2, _, _, _, _ = simplify(token2)
    if isinstance(token1, Constant) and isinstance(token2, Constant):
        comments += [['Converting numbers to Binary Illustrations: ']]
        animations += [[]]
        binaryValue1 = token1.binary()
        binaryValue2 = token2.binary()
        comments += [[]]
        animations += [[tokenizer('a = ' + str(binaryValue1))]]
        comments += [[]]
        animations += [[tokenizer('b = ' + str(binaryValue2))]]
        comments += [['Doing OR operation for each of the consecutive bit']]
        animations += [[]]
        resultValue = token1.calculate() | token2.calculate()
        comments += [['Final result is']]
        animations += [[tokenizer('r = ' + str(resultValue))]]
        token_string = 'r = ' + str(resultValue)
        return token_string, animations, comments
    else:
        return '', [], []


def logicalNOT(token1):
    """Implements Bitwise NOT
    Arguments:
        token1 -- {list} -- List of tokens of a constant number

    Returns:
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    """

    comments = []
    animations = []
    token1, _, _, _, _ = simplify(token1)
    if isinstance(token1, Constant):
        comments += [['Converting numbers to Binary Illustrations: ']]
        animations += [[]]
        binaryValue1 = token1.binary()
        comments += [[]]
        animations += [[tokenizer('a = ' + str(binaryValue1))]]
        resultValueBinary = bin((1 << 8) - 1 - int(binaryValue1, 2))
        resultValue = int(resultValueBinary, 2)
        comments += [['Final binary is']]
        animations += [[tokenizer('r = ' + str(resultValueBinary))]]
        comments += [['Final result is']]
        animations += [[tokenizer('r = ' + str(resultValue))]]
        token_string = 'r = ' + str(resultValue)
        return token_string, animations, comments
    else:
        return '', [], []