{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 17: Number Systems, Boolean Algebra and Digital Circuits"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 1, Page 440"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x=25\n",
      "\n",
      "#Calculations&Results\n",
      "s=int(bin(x)[2:])\n",
      "print \"1. Binary equivalent of 25 is \",s\n",
      "y=576\n",
      "s1=int(bin(y)[2:])\n",
      "print \"2. Binary equivalent of 576 is \",s1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1. Binary equivalent of 25 is  11001\n",
        "2. Binary equivalent of 576 is  1001000000\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2, Page 440"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "s='1111'\n",
      "\n",
      "#Calculations\n",
      "x=int(s,2)\n",
      "\n",
      "#Result\n",
      "print \"Decimal equivalent of 1111 is \",x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal equivalent of 1111 is  15\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 3, Page 440"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "p = 1.;\n",
      "#initialising variables\n",
      "z = 0;\n",
      "b = 0;\n",
      "w = 0;\n",
      "f = 0;\n",
      "bin1 = 11.1101;\n",
      "d = bin1%1;         #separating the decimal part and the integer part\n",
      "d = d*10**10;\n",
      "a = math.floor(bin1);#removing the decimal part\n",
      "b = []\n",
      "while(a>0):#loop to take the binary bits of integer into a matrix\n",
      "    r = a%10;\n",
      "    b.append(r);\n",
      "    a = a/10;\n",
      "    a =  math.floor(a);\n",
      "\n",
      "for m in range(len(b)):#multiplying the bits of integer with their position values and adding\n",
      "    c = m;\n",
      "    f = f+b[m]*(2**c);\n",
      "\n",
      "w = []\n",
      "while(d>0):#loop to take the binary bits of decimal into a matrix\n",
      "    e = d%2\n",
      "    w.append(e)\n",
      "    d = d /10;\n",
      "    d = math.floor(d)\n",
      "\n",
      "for n in range(len(w)):#multiplying the bits of decimal with their position values and adding\n",
      "    z = z+w[n] *(0.5)**(11-n+1);\n",
      "\n",
      "z = z*10000;\n",
      "#rounding of to 4 decimal values\n",
      "z = round(z);\n",
      "z = z/10000;\n",
      "print \"The decimal equivalent of 11.1101 is  =  %.4f\"%(f+z)\n",
      "#answers differ due to usage of in-built functions"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The decimal equivalent of 11.1101 is  =  3.2031\n"
       ]
      }
     ],
     "prompt_number": 63
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4, Page 440"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "q=0.;\n",
      "b=0.;\n",
      "s=0.;\n",
      "a=4.625;\n",
      "\n",
      "#Calculations\n",
      "a=math.floor(a);#removing the decimal part\n",
      "while(a>0):#taking integer part into a matrix and converting into equivalent binary\n",
      "    x=(a%2);\n",
      "    b=b+(10**q)*x;\n",
      "    a=a/2;\n",
      "    a=math.floor(a);\n",
      "    q=q+1;\n",
      "\n",
      "for i in range(1,10):#for values after decimal point converting into binary\n",
      "    d=a*2;\n",
      "    q=math.floor(d);\n",
      "    s=s+q/(10**i);\n",
      "    if d>=1:\n",
      "        d=d-1;\n",
      "\n",
      "k=b+s;\n",
      "\n",
      "#Result\n",
      "print \"The binary equivalent of 4.625 is =\" ,k\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The binary equivalent of 4.625 is = 100.0\n"
       ]
      }
     ],
     "prompt_number": 100
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5, Page 440"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "\n",
      "def toStr(n,base):\n",
      "   convertString = \"0123456789ABCDEF\"\n",
      "   if n < base:\n",
      "      return convertString[n]\n",
      "   else:\n",
      "      return toStr(n//base,base) + convertString[n%base]\n",
      "\n",
      "dec=263\n",
      "base=5\n",
      "\n",
      "#Calculations\n",
      "s=toStr(dec,base)\n",
      "\n",
      "#Result\n",
      "print \"Equivalent of 263 in a code base 5 is \",s"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Equivalent of 263 in a code base 5 is  2023\n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6, Page 441"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x=2\n",
      "\n",
      "#Calculations\n",
      "s=x+x\n",
      "s1=bin(s)\n",
      "\n",
      "#Result\n",
      "print \"Binary addition corresponding to decimal addition 2+2 is \",int(s1[2:])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Binary addition corresponding to decimal addition 2+2 is  100\n"
       ]
      }
     ],
     "prompt_number": 50
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7, Page 441"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x='11111'\n",
      "y='1011'\n",
      "z='101'\n",
      "w='10'\n",
      "v='1'\n",
      "\n",
      "#Calculations\n",
      "s1=int(x,2)\n",
      "s2=int(y,2)\n",
      "s3=int(z,2)\n",
      "s4=int(w,2)\n",
      "s5=int(v,2)\n",
      "a=s1+s2+s3+s4+s5\n",
      "b=int(bin(a)[2:])\n",
      "\n",
      "#Results\n",
      "print \"Binary addition of 11111+1011+101+10+1 is \",b\n",
      "print \"Decimal equivalent corresponding to above binary addition is \",a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Binary addition of 11111+1011+101+10+1 is  110010\n",
        "Decimal equivalent corresponding to above binary addition is  50\n"
       ]
      }
     ],
     "prompt_number": 51
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8, Page 441"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x='1101'\n",
      "y='111'\n",
      "\n",
      "#Calculations\n",
      "s1=int(x,2)\n",
      "s2=int(y,2)\n",
      "a=s1-s2\n",
      "s=int(bin(a)[2:])\n",
      "\n",
      "#Result\n",
      "print \"Binary subtraction 1101-111 is =\",s"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Binary subtraction 1101-111 is = 110\n"
       ]
      }
     ],
     "prompt_number": 52
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9, Page 442"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "hFE=30#hFE=dc current gain of given silicon transistor\n",
      "VBE=0.8#VBE=base-emitter voltage drop at saturation\n",
      "VCE=0.2#VCE=collector-emitter voltage drop at saturation\n",
      "R1=15.*1000#resistance at the base side of the transistor in ohms\n",
      "R2=100*1000#another resistance at the base side of the transistor in ohms\n",
      "RL=2*1000#load resistance at the collector side of the transistor in ohms\n",
      "VCC=10#VCC=collector supply voltage\n",
      "VBB=-10#VBB=base supply voltage\n",
      "\n",
      "#Calculations&Results\n",
      "#If the input level is 0 volt i e vi=0,the open-circuited base voltage is given as\n",
      "VB=VBB*(R1/(R1+R2))\n",
      "print \"For input level 0 V:\"\n",
      "print \"As a bias of approximately 0V is sufficient to cut off a silicon emitter junction, it follows that transistor is cut off when vi=0\"\n",
      "print \"When vi=0,the output voltage is vo=VCC=%.f V\"%VCC\n",
      "print \"This indicates that the output is in state 1 when the input is in state 0\"\n",
      "#When the input level is 10 volt i e vi=10, we have to show that the transistor is in saturation\n",
      "#The minimum base current for saturation is given by iB(min)=iC/hFE\n",
      "iC=(VCC-VCE)/RL#collector current when the transistor saturates\n",
      "iB=iC/hFE#iB=iB(min)=minimum base current for saturation in mA\n",
      "i1=(10-VBE)/R1#i1=current through R1 resistor connected at the base side and here vi=10 is taken\n",
      "i2=(VBE-VBB)/R2#i2=current through R2 resistor connected at the base side\n",
      "iB1=i1-i2#iB1=actual base current\n",
      "print \"\\nFor input level 10 V:\"\n",
      "if (iB1>iB):\n",
      "    print \"Since iB>iB(min),it is verified that the transistor is in saturation\"#iB indicates actual base current & iB(min) indicates minimum base current for saturation\n",
      "    print \"When vi=10,the output voltage is vo=VCE(sat)=%.1f V\"%VCE\n",
      "    print \"This indicates that the output is in state 0 when the input is in state 1\"\n",
      "\n",
      "print \"Overall it has been thus verified that the circuit has performed the NOT operation\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "For input level 0 V:\n",
        "As a bias of approximately 0V is sufficient to cut off a silicon emitter junction, it follows that transistor is cut off when vi=0\n",
        "When vi=0,the output voltage is vo=VCC=10 V\n",
        "This indicates that the output is in state 1 when the input is in state 0\n",
        "\n",
        "For input level 10 V:\n",
        "Since iB>iB(min),it is verified that the transistor is in saturation\n",
        "When vi=10,the output voltage is vo=VCE(sat)=0.2 V\n",
        "This indicates that the output is in state 0 when the input is in state 1\n",
        "Overall it has been thus verified that the circuit has performed the NOT operation\n"
       ]
      }
     ],
     "prompt_number": 56
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10, Page 442"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "A=0\n",
      "B=0\n",
      "\n",
      "#Calculations&Results\n",
      "C=(A|B)#bitwise OR operation is performed\n",
      "print \"Boolean expression C=A+B for inputs A=0 and B=0 is\",C\n",
      "A=1\n",
      "B=0\n",
      "C=(A|B)\n",
      "print \"Boolean expression C=A+B for inputs A=1 and B=0 is\",C\n",
      "A=1\n",
      "B=1\n",
      "C=(A|B)\n",
      "print \"Boolean expression C=A+B for inputs A=1 and B=1 is\",C"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Boolean expression C=A+B for inputs A=0 and B=0 is 0\n",
        "Boolean expression C=A+B for inputs A=1 and B=0 is 1\n",
        "Boolean expression C=A+B for inputs A=1 and B=1 is 1\n"
       ]
      }
     ],
     "prompt_number": 57
    }
   ],
   "metadata": {}
  }
 ]
}