{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 15: Digital Electronics"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.1, Page 771"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "n = 25\n",
      "\n",
      "#Calculations\n",
      "b = bin(n)\n",
      "\n",
      "#Result\n",
      "print \"The binary equivalent of 25 is\",b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The binary equivalent of 25 is 0b11001\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.2, Page 771"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def binary_decimal(ni): # Function to convert binary to decimal\n",
      "    deci = 0;\n",
      "    i = 0;\n",
      "    while (ni != 0):\n",
      "      rem = ni-int(ni/10.)*10\n",
      "      ni = int(ni/10.);\n",
      "      deci = deci + rem*2**i;\n",
      "      i = i + 1;\n",
      "    return deci\n",
      "  \n",
      "\n",
      "def binfrac_decifrac(nf): # Function to convert binary fraction to decimal fraction\n",
      "    decf = 0;\n",
      "    i = -1;\n",
      "    while (i >= -3):\n",
      "      nf = nf*10;\n",
      "      rem = round(nf);    \n",
      "      nf = nf-rem;\n",
      "      decf = decf + rem*2**i;\n",
      "      i = i - 1;\n",
      "    return decf\n",
      "   \n",
      "\n",
      "n = 101.101;    # Initialize the binary number\n",
      "n_int = int(n);     # Extract the integral part\n",
      "n_frac = n-n_int;   # Extract the fractional part\n",
      "\n",
      "#Result\n",
      "print \"Decimal equivalent of %7.3f = %5.3f\"%(n, binary_decimal(n_int)+binfrac_decifrac(n_frac))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal equivalent of 101.101 = 5.625\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.3, Page 772"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def octal_decimal(n): # Function to convert binary to decimal\n",
      "    dec = 0;\n",
      "    i = 0;\n",
      "    while (n != 0):\n",
      "      rem = n-int(n/10)*10; \n",
      "      n = int(n/10);\n",
      "      dec = dec + rem*8**i;\n",
      "      i = i + 1;\n",
      "    return dec\n",
      "\n",
      "n = 173;    # Initialize the octal number\n",
      "\n",
      "#Result\n",
      "print \"Decimal equivalent of %d = %d\"%(n, octal_decimal(n)); \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal equivalent of 173 = 123\n"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.4, Page 772"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "n = 278\n",
      "\n",
      "#Calculations\n",
      "o = oct(n)\n",
      "\n",
      "#Result\n",
      "print \"The octal equivalent of 278 is\",o"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The octal equivalent of 278 is 0426\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.5, Page 772"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "n1 = '10001100'\n",
      "n2 = '1011010111'\n",
      "\n",
      "#Calculations\n",
      "x1 = hex(int(n1,2))\n",
      "x2 = hex(int(n2,2))\n",
      "\n",
      "#Results\n",
      "print \"The hexadecimal equivalent of 10001100 is\",x1\n",
      "print \"The hexadecimal equivalent of 1011010111 is\",x2\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The hexadecimal equivalent of 10001100 is 0x8c\n",
        "The hexadecimal equivalent of 1011010111 is 0x2d7\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.6, Page 772"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "n = 72905\n",
      "\n",
      "#Calculations\n",
      "h = hex(n)\n",
      "\n",
      "#Result\n",
      "print \"The hexadecimal equivalent of 278 is\",h"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The hexadecimal equivalent of 278 is 0x11cc9\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.7, Page 773"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x1 = 0b11101\n",
      "x2 = 0b10111\n",
      "\n",
      "#Calculations\n",
      "x = bin(x1+x2)\n",
      "\n",
      "#Result\n",
      "print \"The required result is\",x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The required result is 0b110100\n"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.8, Page 773"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def decimal_binary(ni): # Function to convert decimal to binary\n",
      "    bini = 0;\n",
      "    i = 1;\n",
      "    while (ni != 0):\n",
      "      rem = ni-int(ni/2)*2; \n",
      "      ni = int(ni/2);\n",
      "      bini = bini + rem*i;\n",
      "      i = i * 10;\n",
      "    return bini\n",
      "\n",
      "def decifrac_binfrac(nf): # Function to convert binary fraction to decimal fraction\n",
      "    binf = 0; i = 0.1;\n",
      "    while (nf != 0):\n",
      "      nf = nf*2;\n",
      "      rem = int(nf); \n",
      "      nf = nf-rem;\n",
      "      binf = binf + rem*i;\n",
      "      i = i/10;\n",
      "    return binf\n",
      "\n",
      "def binary_decimal(ni): # Function to convert binary to decimal\n",
      "    deci = 0;\n",
      "    i = 0;\n",
      "    while (ni != 0):\n",
      "      rem = ni-int(ni/10)*10; \n",
      "      ni = int(ni/10);\n",
      "      deci = deci + rem*2.**i;\n",
      "      i = i + 1;\n",
      "    return deci\n",
      "\n",
      "def binfrac_decifrac(nf): # Function to convert binary fraction to decimal fraction\n",
      "    decf = 0;\n",
      "    i = -1;\n",
      "    while (i >= -3):\n",
      "      nf = nf*10;\n",
      "      rem = round(nf);    \n",
      "      nf = nf-rem;\n",
      "      decf = decf + rem*2.**i;\n",
      "      i = i - 1;\n",
      "    return decf \n",
      "\n",
      "bin1 = 1011.11;    # Initialize the first binary binber\n",
      "bin2 = 1011.01;    # Initialize the second binary binber\n",
      "bin1_int = int(bin1);     # Extract the integral part for first\n",
      "bin1_frac = bin1-bin1_int;   # Extract the fractional part for second\n",
      "bin2_int = int(bin2);     # Extract the integral part for first\n",
      "bin2_frac = bin2-bin2_int;   # Extract the fractional part for second\n",
      "dec1 = binary_decimal(bin1_int)+binfrac_decifrac(bin1_frac);\n",
      "dec2 = binary_decimal(bin2_int)+binfrac_decifrac(bin2_frac);\n",
      "dec = dec1+dec2;\n",
      "dec_int = int(dec);\n",
      "dec_frac = dec-dec_int;\n",
      "\n",
      "#Result\n",
      "print \"%7.2f + %7.2f = %8.2f\"%(bin1, bin2, decimal_binary(dec_int)+decifrac_binfrac(dec_frac))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1011.11 + 1011.01 = 10111.00\n"
       ]
      }
     ],
     "prompt_number": 49
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.9, Page 773"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x1 = 0b0111\n",
      "x2 = 0b1001\n",
      "\n",
      "#Calculations\n",
      "x = bin(x1-x2)\n",
      "\n",
      "#Result\n",
      "print \"The required result is\",x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The required result is -0b10\n"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.10, Page 773"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x1 = 0b1101\n",
      "x2 = 0b1100\n",
      "\n",
      "#Calculations\n",
      "x = bin(x1*x2)\n",
      "\n",
      "#Result\n",
      "print \"The required result is\",x\n",
      "#Incorrect solution in textbook"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The required result is 0b10011100\n"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15.11, Page 774"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "x1 = 0b11001\n",
      "x2 = 0b101\n",
      "\n",
      "#Calculations\n",
      "x = bin(x1/x2)\n",
      "\n",
      "#Result\n",
      "print \"The required result is\",x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The required result is 0b101\n"
       ]
      }
     ],
     "prompt_number": 32
    }
   ],
   "metadata": {}
  }
 ]
}