{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 13: Number Systems" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.1, Page No. 474" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Decimal to binary conversion: 10\n", "\n", "import math\n", "#variable declaration\n", "N = 10 # decimal no to be convered to binary\n", "k = N\n", "#Calculations\n", "i = 0\n", "b =[0,0,0,0,0,0]\n", "while(N!=0):\n", " b[i] = N%2\n", " N = N/2\n", " i = i+1 \n", "\n", "#Result\n", "print(\"Binary equivalent of decimal %d is %d%d%d%d\"%(k,b[3],b[2],b[1],b[0]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Binari equivalent of decimal 10 is 1010\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.2, Page No.474" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Decimal to binary conversion: 25\n", "\n", "import math\n", "#variable declaration\n", "N = 25 # decimal no to be convered to binary\n", "\n", "#Calculations\n", "k = N\n", "i = 0\n", "b =[0,0,0,0,0,0]\n", "while(N!=0):\n", " b[i] = N%2\n", " N = N/2\n", " i = i+1 \n", "\n", "#Result\n", "print(\"Binary equivalent of decimal %d is %d%d%d%d%d\"%(k,b[4],b[3],b[2],b[1],b[0]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Binari equivalent of decimal 25 is 11001\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 13.3, Page No. 474" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Binary to decimal:101110\n", "\n", "import math\n", "b0 = 0 # bit 0\n", "b1 = 1 # bit 1\n", "b2 = 1 # bit 2\n", "b3 = 1 # bit 3\n", "b4 = 0 # bit 4\n", "b5 = 1 # bit 5\n", "\n", "#Calculations\n", "D = (2**0)*b0+(2**1)*b1+(2**2)*b2+(2**3)*b3+(2**4)*b4+(2**5)*b5\n", "\n", "#Result\n", "print(\"Decimal no is %d\"%D)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Decimal no is 46\n" ] } ], "prompt_number": 25 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.4, Page No. 475" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Decimal to binary conversion: 15,31\n", "\n", "import math\n", "#######################################################################################\n", "# for N = 15\n", "#variable declaration\n", "N = 15 # decimal no to be convered to binary\n", "\n", "#Calculations\n", "k = N\n", "i = 0\n", "b =[0,0,0,0,0,0]\n", "while(N!=0):\n", " b[i] = N%2\n", " N = N/2\n", " i = i+1 \n", "\n", "#Result\n", "print(\"(a)\\nBinary equivalent of decimal %d is %d%d%d%d%d\"%(k,b[4],b[3],b[2],b[1],b[0]))\n", "\n", "########################################################################################\n", "\n", "# For N =31\n", "#variable declaration\n", "N = 31 # decimal no to be convered to binary\n", "\n", "#Calculations\n", "k = N\n", "i = 0\n", "b =[0,0,0,0,0,0]\n", "while(N!=0):\n", " b[i] = N%2\n", " N = N/2\n", " i = i+1 \n", "\n", "#Result\n", "print(\"Binary equivalent of decimal %d is %d%d%d%d%d\"%(k,b[4],b[3],b[2],b[1],b[0]))\n", "##########################################################################################\n", "#Addition\n", "c= bin(15+31)[2:]\n", "print(\"(b) Addition of 15 and 31 is %s in binary. Decimal equivalent of %s is %d\"%(c,c,int(c,2)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(a)\n", "Binari equivalent of decimal 15 is 01111\n", "Binari equivalent of decimal 31 is 11111\n", "(b) Addition of 15 and 31 is 101110 in binary. Decimal equivalent of 101110 is 46\n" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.5, Page No.475" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Binary subtraction\n", "\n", "import math\n", "#Variable declaration\n", "n1 = 11001 # first number\n", "n2 = 10001 # second number\n", "\n", "#calculations\n", "n1 = int(str(n1),2)\n", "n2 = int(str(n2),2)\n", "c = bin(n1-n2)[2:]\n", "\n", "#Result\n", "print(\"Subtraction result in binary = %s\"%c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Subtraction result in binary = 1000\n" ] } ], "prompt_number": 42 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.6, Page No.475" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Binary subtraction\n", "\n", "import math\n", "#Variable declaration\n", "n1 = 1010 # first number\n", "n2 = 111 # second number\n", "\n", "#calculations\n", "n1 = int(str(n1),2)\n", "n2 = int(str(n2),2)\n", "c = bin(n1-n2)[2:]\n", "\n", "#Result\n", "print(\"Subtraction result in binary = %s\"%c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Subtraction result in binary = 11\n" ] } ], "prompt_number": 41 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.7, Page No. 476" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 16- bit signed binary representation\n", "\n", "import math\n", "#Variable declaration\n", "a = 8\n", "b = -8\n", "c = 165\n", "d = -165\n", "\n", "#Calculations\n", "#c = bin(d)\n", "a = format(a,'#018b')[2:]\n", "b = format(b,'#018b')[3:]\n", "c = format(c,'#018b')[2:]\n", "d = format(d,'#018b')[3:]\n", "str = '1'\n", "#Result\n", "print(\"In the leading bit we will have 1 to represent '-' sign\\n\")\n", "print(\"(a) +8 --> %s\"%(a))\n", "print(\"(b) -8 --> %s%s\"%(str,b))\n", "print(\"(c) +167 --> %s\"%(c))\n", "print(\"(d) -167 --> %s%s\"%(str,d))\n", "\n", "a = format(-167%(1<<16),'016b')\n", "b = format(167%(1<<16),'016b')\n", "print a\n", "print b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In the leading bit we will have 1 to represent '-' sign\n", "\n", "(a) +8 --> 0000000000001000\n", "(b) -8 --> 1000000000001000\n", "(c) +167 --> 0000000010100101\n", "(d) -167 --> 1000000010100101\n", "1111111101011001\n", "0000000010100111\n" ] } ], "prompt_number": 87 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.8, Page No.477" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 2's complement\n", "\n", "import math\n", "# variable declaration\n", "a = int('00011111',2)\n", "b = int('11100101',2)\n", "c = int('11110111',2)\n", "\n", "#Calculations\n", "a = format(-a%(1<<8),'08b')\n", "b = format(-b%(1<<8),'08b')\n", "c = format(-c%(1<<8),'08b')\n", "print(\"(a) 2's complement of 00011111 --> %s \" %a)\n", "print(\"(b) 2's complement of 11100101 --> %s \" %b)\n", "print(\"(c) 2's complement of 11110111 --> %s \" %c)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(a) 2's complement of 00011111 --> 11100001 \n", "(b) 2's complement of 11100101 --> 00011011 \n", "(c) 2's complement of 11110111 --> 00001001 \n" ] } ], "prompt_number": 95 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.9, Page No. 13.9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 8-bit number range\n", "\n", "import math\n", "#calculations\n", "a = int('01111111',2)\n", "b = int('10000000',2)\n", "\n", "#Result\n", "print(\"largest positive 8-bit no in %d and smallest negative number is -%d\"%(a,b))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "largest positive 8-bit no in 127 and smallest negative number is -128\n" ] } ], "prompt_number": 97 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.10, Page No.10" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 2's complement and addition,subtraction\n", "\n", "import math\n", "#Variable declaration\n", "A = -24\n", "B = 16\n", "#Calculations\n", "#2's complement\n", "a = format(A%(1<<8),'08b')\n", "b = format(-B%(1<<8),'08b')\n", "# addition\n", "c = bin(A+B)[3:]\n", "c = format(-int(c,2)%(1<<8),'08b')\n", "#Subtraction\n", "d = bin(A-B)[3:]\n", "d = format(-int(d,2)%(1<<8),'08b')\n", "#Result\n", "print(\"(a)\\n2's complement of -24 --> %s \" %a)\n", "print(\"2's complement of 16 --> %s \" %b)\n", "print(\"(b) A+B = %s\"%c) \n", "print(\"(c) A-B = %s\"%d)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(a)\n", "2's complement of -24 --> 11101000 \n", "2's complement of 16 --> 11110000 \n", "(b) A+B = 11111000\n", "(c) A-B = 11011000\n" ] } ], "prompt_number": 116 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.11, Page No. 479" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 2's complement and addition,subtraction\n", "\n", "import math\n", "#Variable declaration\n", "A = -60\n", "B = -28\n", "#Calculations\n", "#2's complement\n", "a = format(A%(1<<8),'08b')\n", "b = format(B%(1<<8),'08b')\n", "# addition\n", "c = bin(A+B)[3:]\n", "c = format(-int(c,2)%(1<<8),'08b')\n", "#Subtraction\n", "x = B-A\n", "d = bin(B-A)[2:]\n", "d = format(int(d,2),'08b')\n", "#Result\n", "print(\"(a)\\n2's complement of A --> %s \" %a)\n", "print(\"2's complement of B --> %s \" %b)\n", "print(\"(b) B+A = %s\"%c) \n", "print(\"(c) B-A = %s\"%d)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(a)\n", "2's complement of A --> 11000100 \n", "2's complement of B --> 11100100 \n", "(b) B+A = 10101000\n", "(c) B-A = 00100000\n" ] } ], "prompt_number": 130 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.12, Page No. 479" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# decimal to binary\n", "\n", "# For N = 0.6875\n", "#variable declaration\n", "N = 0.6875 # decimal no to be convered to binary\n", "\n", "#Calculations\n", "k = N\n", "i = 0\n", "b =[0,0,0,0,0,0,0,0,0,0,0,0]\n", "while(N!=0):\n", " N = N*2\n", " #print N\n", " b[i]=0\n", " #print b[i]\n", " if (N>1) or(N==1):\n", " N =N-1\n", " b[i]=1\n", " i=i+1\n", " \n", "#Result\n", "print(\"Binary equivalent of decimal %f is 0.%d%d%d%d \"%(k,b[0],b[1],b[2],b[3]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Binary equivalent of decimal 0.687500 is 0.1011 \n" ] } ], "prompt_number": 230 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.13, Page No.480" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# decimal to binary\n", "\n", "# For N = 0.634\n", "#variable declaration\n", "N = 0.634 # decimal no to be convered to binary\n", "\n", "#Calculations\n", "k = N\n", "i = 0\n", "b =[0,0,0,0,0,0,0,0,0,0,0,0]\n", "while(i!=7):\n", " N = N*2\n", " #print N\n", " b[i]=0\n", " #print b[i]\n", " if (N>1) or(N==1):\n", " N =N-1\n", " b[i]=1\n", " i=i+1\n", "\n", "#Result \n", "#Result\n", "print(\"Binary equivalent of decimal %f is 0.%d%d%d%d%d%d%d \"%(k,b[0],b[1],b[2],b[3],b[4],b[5],b[6]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Binary equivalent of decimal 0.634000 is 0.1010001 \n" ] } ], "prompt_number": 222 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.14, Page No.480" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# decimal to binary\n", "\n", "# For N = 0.634\n", "#variable declaration\n", "N = 39.12 # decimal no to be convered to binary\n", "\n", "N1 = 39\n", "k = N1\n", "#Calculations\n", "i = 0\n", "b =[0,0,0,0,0,0,0,0]\n", "while(N1!=0):\n", " b[i] = N1%2\n", " N1 = N1/2\n", " i = i+1 \n", "\n", "#Result\n", "print(\"Binary equivalent of decimal %d is %d%d%d%d%d%d%d\"%(k,b[6],b[5],b[4],b[3],b[2],b[1],b[0]))\n", "\n", "\n", "N2 =0.12\n", "#Calculations\n", "k = N2\n", "i = 0\n", "b =[0,0,0,0,0,0,0,0,0,0,0,0]\n", "while(i!=7):\n", " N2 = N2*2\n", " #print N\n", " b[i]=0\n", " #print b[i]\n", " if (N2>1) or(N2==1):\n", " N2 =N2-1\n", " b[i]=1\n", " i=i+1\n", " \n", "#Result\n", "print(\"Binary equivalent of decimal %f is 0.%d%d%d%d%d%d%d \"%(k,b[0],b[1],b[2],b[3],b[4],b[5],b[6]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Binary equivalent of decimal 39 is 0100111\n", "Binary equivalent of decimal 0.120000 is 0.0001111 \n" ] } ], "prompt_number": 225 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.15, Page No.481" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# binary Addition\n", "\n", "import math\n", "#Variable declaration\n", "a1 = int('101101',2) # integer part of first no\n", "a2 = int('0101',2) # fractiona part of first no\n", "b1 = int('10001',2) # integer part of second no\n", "b2 = int('1010',2) # fractiona part of second no(in fraction we can add any no of 0s)\n", "\n", "#Calculations\n", "c1= bin(a1+b1)[2:]\n", "c2 = bin(a2+b2)[2:]\n", "\n", "print(\"Addition --> %s.%s\"%(c1,c2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Addition --> 111110.1111\n" ] } ], "prompt_number": 227 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.16, Page No. 481" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# binary to decimal conversion\n", "\n", "import math\n", "#Variable declaration\n", "i0 = 1 # LSB of integer\n", "i1 = 0\n", "i2 = 0\n", "i3 = 1\n", "i4 = 1 # MSB of integer\n", "f1 = 0\n", "f2 = 0\n", "f3 = 1\n", "f4 = 0\n", "f5 = 1\n", "f6 = 1\n", "\n", "#Calculations\n", "D = i0*(2**0)+i1*(2**1)+i2*(2**2)+i3*(2**3)+i4*(2**4)+f1*(2**-1)+f2*(2**-2)+f3*(2**-3)+f4*(2**-4)+f5*(2**-5)+f6*(2**-6)\n", "\n", "#Result\n", "print(\"Decimal equivalent = %f\"%D)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Decimal equivalent = 25.171875\n" ] } ], "prompt_number": 231 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.17, Page No. 482" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Hexadecimal to decimal\n", "\n", "import math\n", "#variable declaration\n", "a = 3\n", "b = 10 # decimal equivalent of A\n", "c = 8\n", "#Calculations\n", "# binary equvalent of 8 A 3 \n", "# 1000 1010 0011\n", "# a\n", "Da = (1*2**0)+(1*2**1)+(0*2**2)+(0*2**3)+(0*2**4)+(1*2**5)+(0*2**6)+(1*2**7)+(0*2**8)+(0*2**9)+(0*2**10)+(1*2**11)\n", "print(\"(a) Decimal equivalent of 8A3 is %d\"%Da)\n", "# b \n", "Db = (a*(16**0))+(b*(16**1))+(c*(16**2))\n", "print(\"(b) Decimal equivalent of 8A3 is %d\"%Db)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(a) Decimal equivalent of 8A3 is 2211\n", "(b) Decimal equivalent of 8A3 is 2211\n" ] } ], "prompt_number": 236 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.18, Page No. 482" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# decimal to hexadecimal\n", "\n", "import math\n", "#Variable declaration\n", "N = 268 # no to be converted into hexadecimal\n", "\n", "N1 = 268\n", "k = N1\n", "#Calculations\n", "i = 0\n", "b =[0,0,0,0,0,0,0,0]\n", "while(N1!=0):\n", " b[i] = N1%16\n", " N1 = N1/16\n", " i = i+1 \n", "\n", "#Result\n", "print(\"Remainder1: %d = C\\nRemainder2: %d\\nRemainder3: %d\\n\\nHexaecimal equivalent of %d = 10C\"%(b[0],b[1],b[2],k))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Remainder1: 12 = C\n", "Remainder2: 0\n", "Remainder3: 1\n", "\n", "Hexaecimal equivalent of 268 = 10C\n" ] } ], "prompt_number": 260 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.19, Page No.483" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# decimal to hexadecimal\n", "\n", "import math\n", "#Variable declaration\n", "N = 5741 # no to be converted into hexadecimal\n", "\n", "N1 = 5741\n", "k = N1\n", "#Calculations\n", "i = 0\n", "b =[0,0,0,0,0,0,0,0]\n", "while(N1!=0):\n", " b[i] = N1%16\n", " N1 = N1/16\n", " i = i+1 \n", "\n", "#Result\n", "print(\"Remainder1: %d = D\\nRemainder2: %d\\nRemainder3: %d\\nRemainder4: %d\\n\\nHexaecimal equivalent of %d =166D\"%(b[0],b[1],b[2],b[3],k))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Remainder1: 13 = D\n", "Remainder2: 6\n", "Remainder3: 6\n", "Remainder4: 1\n", "\n", "Hexaecimal equivalent of 5741 =166D\n" ] } ], "prompt_number": 259 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.20, Page No.483" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Hexadecimal to decimal\n", "\n", "import math\n", "#variable declaration\n", "a = 0\n", "b = 7\n", "c = 13 # decimal equivalent of D\n", "#Calculations\n", "D = (a*(16**0))+(b*(16**1))+(c*(16**2))\n", "\n", "#Result\n", "print(\"(b) Decimal equivalent of 8A3 is %d\"%D)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(b) Decimal equivalent of 8A3 is 3440\n" ] } ], "prompt_number": 262 } ], "metadata": {} } ] }