# nand test: # see http://en.wikipedia.org/wiki/Logical_NAND # (A nand B) is not (A and B) ABList = [(False,False),(False,True),(True,False),(True,True)] for A,B in ABList: print '%s nand %s ='%(str(A),str(B)),not (A and B) """ Exercise 2.1 Scientific Computing Thu 3 Oct 2013 10:46:58 BST P. Lewis : p.lewis@ucl.ac.uk nand test see http://en.wikipedia.org/wiki/Logical_NAND (A nand B) is not (A and B) It is a modification of the code in Exercise A2.1 of Scientific Computing (https://github.com/profLewis/geogg122) Edits made from original: - neatened output format """ # set up a list of tuples to loop over ABList = [(False,False),(False,True),(True,False),(True,True)] # loop over tuples and print A xor B # with tabs (\t) for neater output for A,B in ABList: print '%s\tnand\t%s\t='%(str(A),str(B)),not (A and B) """ Exercise 2.1 Scientific Computing Thu 3 Oct 2013 10:46:58 BST P. Lewis : p.lewis@ucl.ac.uk nor test see http://en.wikipedia.org/wiki/Logical_NOR (A nor B) is not (A or B) It is a modification of the code in Exercise A2.1 of Scientific Computing (https://github.com/profLewis/geogg122) Edits made from original: - neatened output format - modified from nand to nor """ # set up a list of tuples to loop over ABList = [(False,False),(False,True),(True,False),(True,True)] # loop over tuples and print A xor B # with tabs (\t) for neater output for A,B in ABList: print '%s\tnor\t%s\t='%(str(A),str(B)),not (A or B) """ Exercise 2.1 Scientific Computing Thu 3 Oct 2013 10:46:58 BST P. Lewis : p.lewis@ucl.ac.uk xor test see http://en.wikipedia.org/wiki/Logical_XOR (A xor B) is (A or B) and not (A and B) It is a modification of the code in Exercise A2.1 of Scientific Computing (https://github.com/profLewis/geogg122) Edits made from original: - neatened output format - modified from nand to xor """ # set up a list of tuples to loop over ABList = [(False,False),(False,True),(True,False),(True,True)] # loop over tuples and print A xor B # with tabs (\t) for neater output for A,B in ABList: print '%s\txor\t%s\t='%(str(A),str(B)),(A or B) and not (A and B) for n in (7,493,127,255,1024): print n,'\tin binary is',bin(n) 7 == (1 * 2**2) + \ (1 * 2**1) + \ (1 * 2**0) 493 == (1 * 2**8) + \ (1 * 2**7) + \ (1 * 2**6) + \ (1 * 2**5) + \ (0 * 2**4) + \ (1 * 2**3) + \ (1 * 2**2) + \ (0 * 2**1) + \ (1 * 2**0) 127 == (1 * 2**6) + \ (1 * 2**5) + \ (1 * 2**4) + \ (1 * 2**3) + \ (1 * 2**2) + \ (1 * 2**1) + \ (1 * 2**0) 255 == (1 * 2**7) + \ (1 * 2**6) + \ (1 * 2**5) + \ (1 * 2**4) + \ (1 * 2**3) + \ (1 * 2**2) + \ (1 * 2**1) + \ (1 * 2**0) 1024 == (1 * 2**10)+ \ (0 * 2**9) + \ (0 * 2**8) + \ (0 * 2**7) + \ (0 * 2**6) + \ (0 * 2**5) + \ (0 * 2**4) + \ (0 * 2**3) + \ (0 * 2**2) + \ (0 * 2**1) + \ (0 * 2**0) # loop over numbers for n in (7,493,127,255,1024): # convert decimal to binary # then convert to string and ignore 0b at start binstr = bin(n)[2:] # initialise sum as accumulator sum = 0 # how many bits in this case? nBits = len(binstr) print n,'\tin binary is',binstr,": %d bits"%nBits # loop over each bit for c in xrange(nBits): # extract the bit and exponent bit = int(binstr[-(c+1)]) exp = 2**c sum += exp * bit print '\t %d x 2^%d = %d x %d \t%d'%(bit,c,bit,exp,sum) print "%d == %d?"%(n,sum),n == sum print "====================================" n = 493 # N.B, no 0b or 0x part on the string for octal, just a # leading 0 octstr = oct(n)[1:] sum = 0 # how many octets in this case? (assuming thats the right word) nOct = len(octstr) print n,'\tin octal is',octstr,": %d octets"%nOct # loop over each octets for c in xrange(nOct): # extract the octet and exponent octet = int(octstr[-(c+1)]) exp = 8**c sum += exp * octet print '\t %d x 8^%d = %d x %d \t%d'%(octet,c,octet,exp,sum) print "%d == %d?"%(n,sum),n == sum print "====================================" # bin() converts to binary, but really its a string # the first 2 characters of which are '0b' # so find the length of the string other than the first two # characters for n in (7,493,127,255,1024): binstr = bin(n)[2:] print binstr,len(binstr),'bits' print "the largest (unsigned) integer with 8 bits (1 byte) is",2**8 -1 print "the largest (unsigned) integer with 32 bits is",2**32 -1 print "the largest (unsigned) integer with 64 bits is",2**64 -1 print "the largest (signed) integer with 8 bits (1 byte) is",+2**(8-1) -1 print "the smallest (signed) integer with 8 bits (1 byte) is",-2**(8-1) +1 print "the largest (unsigned) integer with 16 bits (2 bytes) is",2**16 -1 print "the largest (signed) integer with 16 bits (2 bytes) is",+2**(16-1) -1 print "the smallest (signed) integer with 16 bits (2 bytes) is",-2**(16-1) +1 qa = 0b11111111 qa1 = (qa & 0b00000001) >> 0 # bit 0 qa2 = (qa & 0b00000010) >> 1 # bit 1 qa3 = (qa & 0b00000100) >> 2 # bit 2 qa4 = (qa & 0b00011000) >> 3 # bit 3-4 qa5 = (qa & 0b11100000) >> 5 # bit 5-7 print 'qa1',bin(qa1) print 'qa2',bin(qa2) print 'qa3',bin(qa3) print 'qa4',bin(qa4) print 'qa5',bin(qa5) mask2 = 0b1 << 2 print bin(mask2) mask3 = 0b11 << 3 print bin(mask3) qa = 0b00011000 mask3 = 0b11 print bin((qa >> 3) & mask3) fields = {0:0, 1:1, 2:2, 3:4, 5:7} for start,finish in fields.items(): print start,finish # a test qa with only required bits qa = 0b00011000 start = 3 finish = 4 print bin(2**(finish-start+1)-1) # qa with all bits filled qa = 0b11111111 for start,finish in fields.items(): mask = 2**(finish-start+1)-1 print start,finish,bin(mask) start = 3 finish = 4 qa = 0b00011000 mask = (2**(finish-start+1)-1) maskedQA = (qa >> start) & mask print bin(maskedQA) fields = {0:0, 1:1, 2:2, 3:4, 5:7} # qa with all bits filled qa = 0b11111111 for start,finish in fields.items(): mask = (2**(finish-start+1)-1) maskedQA = (qa >> start) & mask print start,finish,bin(maskedQA) """Exercise 2.1 Scientific Computing Thu 3 Oct 2013 10:46:58 BST P. Lewis : p.lewis@ucl.ac.uk bit mask test Original code """ def maskedQA(qa, fields = {0:0, 1:1, 2:2, 3:4, 5:7}): """Return a dictionary of masked QA bit fields Inputs: qa -- QA to be masked Keyword arguments: fields -- bit field dictionary (default {0:0, 1:1, 2:2, 3:4, 5:7}) Here, the key is the start bit of a mask and the value the end bit (inclusive). Returns: Masked QA in dictionary with same keys as fields """ maskedQAs = {} for start,finish in fields.items(): mask = (2**(finish-start+1)-1) maskedQAs[start] = (qa >> start) & mask return maskedQAs help(maskedQA) # testing qaDict = maskedQA(0b11111111) for k in qaDict.keys(): print k,qaDict[k],bin(qaDict[k]) qaDict = maskedQA(0b01010101) for k in qaDict.keys(): print k,qaDict[k],bin(qaDict[k])