{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 9: Digital Electronics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.8, Page 176"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decimal Equivalent = 26.000000\n"
     ]
    }
   ],
   "source": [
    "#Initialization\n",
    "ni1=11010               #binary number\n",
    "\n",
    "#Calculation\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",
    "w=binary_decimal(ni1)              #calling the function\n",
    "\n",
    "#Declaration\n",
    "print'Decimal Equivalent = %f'%w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.9, Page 176"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Binary Equivalent = 11010\n"
     ]
    }
   ],
   "source": [
    "#Initialization\n",
    "ni1=26                #Decimal number\n",
    "\n",
    "#Calculation\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",
    "w=decimal_binary(ni1)              #calling the function\n",
    "\n",
    "#Declaration\n",
    "print'Binary Equivalent = %d'%w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.10, Page 177"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decimal equivalent of 34.6875 = 100010.1011\n"
     ]
    }
   ],
   "source": [
    "#Initializaton\n",
    "\n",
    "no=34.6875            #decimal number\n",
    "n_int = int(no);     # Extract the integral part\n",
    "n_frac = no-n_int;   # Extract the fractional part\n",
    "\n",
    "#Calculation\n",
    "\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",
    "\n",
    "\n",
    "#Result\n",
    "print \"Decimal equivalent of 34.6875 = %.4f\"%(decimal_binary(n_int)+decifrac_binfrac(n_frac))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.11, Page 177"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W = 40979\n"
     ]
    }
   ],
   "source": [
    "#initialization\n",
    "n='A013'                 #Hex number \n",
    "\n",
    "#Calculation\n",
    "w=int(n, 16)              #Hex to Decimal Coversion\n",
    "\n",
    "\n",
    "#Result\n",
    "print'W = %d'%w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.12, Page 178"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The hexadecimal equivalent of 7046 is 0x1b86\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#Variable declaration\n",
    "n=7046                 #Hex number \n",
    "\n",
    "#Calculations\n",
    "h = hex(n)                             #decimal to hex conversion\n",
    "\n",
    "#Result\n",
    "print \"The hexadecimal equivalent of 7046 is\",h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.13, Page 178"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decimal equivalent of 34.6875 = 1111100001010001\n"
     ]
    }
   ],
   "source": [
    "#Initializaton\n",
    "\n",
    "n='f851'                   #Hex Number\n",
    "\n",
    "#Calculation\n",
    "\n",
    "w=int(n, 16)              #Hex to Decimal Coversion\n",
    "\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",
    "\n",
    "w1=decimal_binary(w)              #calling the function\n",
    "\n",
    "\n",
    "#Result\n",
    "print \"Decimal equivalent of 34.6875 = %.d\"%(w1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.14, Page 179"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The hexadecimal equivalent of 111011011000100 is 0x76c4\n"
     ]
    }
   ],
   "source": [
    "#Initialiation\n",
    "ni1=111011011000100               #binary number\n",
    "\n",
    "#Calculation\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",
    "w=binary_decimal(ni1)              #calling the function\n",
    "h = hex(w)                             #decimal to hex conversion\n",
    "\n",
    "#Result\n",
    "print \"The hexadecimal equivalent of 111011011000100 is\",h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Example 9.15, Page 182"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eqivalent BCD of 72 =  1001010001010000\n"
     ]
    }
   ],
   "source": [
    "#initialisation\n",
    "x='9450'                           #decimal number to be convert\n",
    "\n",
    "#calculation\n",
    "digits = [int(c) for c in x]\n",
    "zero_padded_BCD_digits = [format(d, '04b') for d in digits]\n",
    "\n",
    "#results\n",
    "print \"Eqivalent BCD of 72 = \",\n",
    "print ''.join(zero_padded_BCD_digits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.16, Page 182"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The equivalent decimal =3876.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#Initialisation\n",
    "BCD=\"0011 1000 0111 0110\"             #Given BCD string\n",
    "BCD_split=BCD.split(\" \");        #Splitting th binary string into individual BCD \n",
    "d=0;\n",
    "for i in range(len(BCD_split),0,-1):\n",
    "    d+=int(BCD_split[len(BCD_split)-i],2)*10**(i-1);\n",
    "\n",
    "#Result\n",
    "print(\"The equivalent decimal = %d.\"%d);\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}