cintop.pyx 1.09 KB
Newer Older
Stefan Behnel's avatar
Stefan Behnel committed
1 2
# mode: run

3
__doc__ = u"""
4 5 6 7 8 9 10 11 12 13 14
    >>> int2 = 42
    >>> int3 = 7
    >>> char1 = ord('C')

    >>> int1 = int2 | int3
    >>> int1 |= int2 ^ int3
    >>> int1 ^= int2 & int3
    >>> int1 ^= int2 << int3
    >>> int1 ^= int2 >> int3
    >>> int1 ^= int2 << int3 | int2 >> int3
    >>> long1 = char1 | int1
Stefan Behnel's avatar
Stefan Behnel committed
15
    >>> (int1, long1) == f()
16 17
    True

Stefan Behnel's avatar
Stefan Behnel committed
18 19
"""

20
def f():
21 22 23 24
    """
    >>> f()
    (45, 111)
    """
25 26 27
    cdef int int1, int2, int3
    cdef char char1
    cdef long long1, long2
Stefan Behnel's avatar
Stefan Behnel committed
28 29 30 31
    int2 = 42
    int3 = 7
    char1 = c'C'

32
    int1 = int2 | int3
33 34 35 36 37 38
    int1 |= int2 ^ int3
    int1 ^= int2 & int3
    int1 ^= int2 << int3
    int1 ^= int2 >> int3
    int1 ^= int2 << int3 | int2 >> int3
    long1 = char1 | int1
Stefan Behnel's avatar
Stefan Behnel committed
39
    return int1, long1
Stefan Behnel's avatar
Stefan Behnel committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


def long_int_shift():
    """
    >>> long_int_shift()
    80082
    10010
    10010
    10010
    10010
    """
    value = 80082 # int using more than 2 bytes == long
    print(value)
    shiftedby3 = value >> 3
    dividedby8 = value // 8
    print(shiftedby3)
    print(dividedby8)
    shiftedby3 = 80082 >> 3
    dividedby8 = 80082 // 8
    print(shiftedby3)
    print(dividedby8)