<s> __author__ = """<STR_LIT>""" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from os import getcwd <EOL> from os import sys <EOL> sys . path . append ( getcwd ( ) ) <EOL> from MOAL . helpers . display import Section <EOL> from MOAL . helpers . display import divider <EOL> from MOAL . helpers . text import random_binary <EOL> from MOAL . computer_organization . numerical_encoding_basic import bin_to_dec <EOL> from MOAL . computer_organization . numerical_encoding_basic import dec_to_bin <EOL> from MOAL . computer_organization . data_types import BaseDataType <EOL> from random import choice <EOL> DEBUG = True if __name__ == '<STR_LIT:__main__>' else False <EOL> def ones_complement ( binary ) : <EOL> binary = list ( binary ) <EOL> for k , digit in enumerate ( binary ) : <EOL> binary [ k ] = '<STR_LIT:1>' if digit == '<STR_LIT:0>' else '<STR_LIT:0>' <EOL> return '<STR_LIT>' . join ( binary ) <EOL> def twos_complement ( binary ) : <EOL> """<STR_LIT>""" <EOL> old = binary <EOL> binary = ones_complement ( binary ) <EOL> ones = binary <EOL> binary = BaseDataType . increment ( '<STR_LIT>' . join ( binary ) ) <EOL> if DEBUG : <EOL> print ( '<STR_LIT>' . format ( <EOL> '<STR_LIT>' . join ( ones ) , binary , '<STR_LIT>' . join ( old ) ) ) <EOL> dec = bin_to_dec ( binary ) <EOL> sign , res = ( '<STR_LIT>' , - dec ) if list ( binary ) [ <NUM_LIT:0> ] == '<STR_LIT:1>' else ( '<STR_LIT>' , dec , ) <EOL> res_bin = dec_to_bin ( res ) <EOL> if DEBUG : <EOL> print ( '<STR_LIT>' . format ( sign , res , res_bin ) ) <EOL> return res_bin <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> with Section ( '<STR_LIT>' ) : <EOL> evens = [ n for n in range ( <NUM_LIT:2> , <NUM_LIT:16> ) if n % <NUM_LIT:4> == <NUM_LIT:0> ] <EOL> for _ in range ( <NUM_LIT:8> ) : <EOL> twos_complement ( random_binary ( choice ( evens ) ) ) <EOL> divider ( atom = '<STR_LIT:->' ) </s>
