{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ch-24 : Digital Circuits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page No. 645 Example 24.1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The procedure is as follows.\n",
      "12 divided by 8 = quotient 1 with a remainder of 4\n",
      "1 divided by 8 = quotient 0 with a remainder of 1\n",
      "Therefore,  decimal 12 = octal 14\n"
     ]
    }
   ],
   "source": [
    "#convert decimal 12 to an octal number\n",
    "def base10toN(num, base):\n",
    "    \"\"\"Change ``num'' to given base\n",
    "    Upto base 36 is supported.\"\"\"\n",
    "\n",
    "    converted_string, modstring = \"\", \"\"\n",
    "    currentnum = num\n",
    "    if not 1 < base < 37:\n",
    "        raise ValueError(\"base must be between 2 and 36\")\n",
    "    if not num:\n",
    "        return '0'\n",
    "    while currentnum:\n",
    "        mod = currentnum % base\n",
    "        currentnum = currentnum // base\n",
    "        converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string\n",
    "    return converted_string\n",
    "\n",
    "o=base10toN(12,8)\n",
    "print \"The procedure is as follows.\"\n",
    "print \"12 divided by 8 = quotient 1 with a remainder of 4\"\n",
    "print \"1 divided by 8 = quotient 0 with a remainder of 1\"\n",
    "print \"Therefore,  decimal 12 = octal\",o"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page No. 646 Example 24.2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(i) octal 144 = decimal 100\n",
      "(ii) octal 237 = decimal 159\n",
      "(iii) octal 120 = decimal 80\n"
     ]
    }
   ],
   "source": [
    "# convert octal number to decimal.\n",
    "d=int('144',8)\n",
    "print \"(i) octal 144 = decimal\",d\n",
    "d1=int(\"237\",8)\n",
    "print \"(ii) octal 237 = decimal\",d1\n",
    "d2=int('120',8)\n",
    "print \"(iii) octal 120 = decimal\",d2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page No. 647 Example 24.3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The procedure is as follows,\n",
      "(i) 112 divided by 16 = quotient 7 with a remainder of 0\n",
      "      7 divided by 16 = quotient 0 with a remainder of 7\n",
      "decimal 112 = hex 70\n",
      "(ii) 253 divided by 16 = quotient 7 with a remainder of 13 i.e. D\n",
      "      15 divided by 16 = quotient 0 with a remainder of 15 i.e. F\n",
      "decimal 253 = hex FD\n"
     ]
    }
   ],
   "source": [
    "#convert decimal to hexadecimal number\n",
    "def base10toN(num, base):\n",
    "    \"\"\"Change ``num'' to given base\n",
    "    Upto base 36 is supported.\"\"\"\n",
    "\n",
    "    converted_string, modstring = \"\", \"\"\n",
    "    currentnum = num\n",
    "    if not 1 < base < 37:\n",
    "        raise ValueError(\"base must be between 2 and 36\")\n",
    "    if not num:\n",
    "        return '0'\n",
    "    while currentnum:\n",
    "        mod = currentnum % base\n",
    "        currentnum = currentnum // base\n",
    "        converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string\n",
    "    return converted_string\n",
    "\n",
    "h=base10toN(112,16)\n",
    "print \"The procedure is as follows,\"\n",
    "print \"(i) 112 divided by 16 = quotient 7 with a remainder of 0\"\n",
    "print \"      7 divided by 16 = quotient 0 with a remainder of 7\"\n",
    "print \"decimal 112 = hex\",h\n",
    "print \"(ii) 253 divided by 16 = quotient 7 with a remainder of 13 i.e. D\"\n",
    "print \"      15 divided by 16 = quotient 0 with a remainder of 15 i.e. F\"\n",
    "h=base10toN(253,16)\n",
    "print \"decimal 253 = hex\",h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page No. 648 Example 24.4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(i) hex 4AB = decimal 1195.0\n",
      "(ii) hex 23F = decimal 575.0\n"
     ]
    }
   ],
   "source": [
    "#convert hexadecimal number to decimal\n",
    "h=float.fromhex('4AB')\n",
    "print \"(i) hex 4AB = decimal\",h\n",
    "h=float.fromhex('23F')\n",
    "print \"(ii) hex 23F = decimal\",h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page No. 650 Example 24.5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(i) 1101 x 1100 = 10011100\n",
      "(ii) 1000 x 101 = 101000\n",
      "(iii) 1111 x 1001 = 10000111\n"
     ]
    }
   ],
   "source": [
    "# multiply binary numbers\n",
    "h=int('1101',2)\n",
    "o=int('1100',2)\n",
    "p=h*o\n",
    "z=bin(p)[2:]\n",
    "print \"(i) 1101 x 1100 =\",z\n",
    "h=int('1000',2)\n",
    "o=int('101',2)\n",
    "p=h*o\n",
    "z=bin(p)[2:]\n",
    "print \"(ii) 1000 x 101 =\",z\n",
    "h=int('1111',2)\n",
    "o=int('1001',2)\n",
    "p=h*o\n",
    "z=bin(p)[2:]\n",
    "print \"(iii) 1111 x 1001 =\",z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page No. 651 Example 24.6."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(i) 110 / 10\n",
      "  = binary 11\n",
      "  = decimal 3\n",
      "(ii) 1111 / 110\n",
      "  = binary 10\n",
      "  = decimal) 2\n"
     ]
    }
   ],
   "source": [
    "# perform the binary divisions\n",
    "\n",
    "x=int('110',2)\n",
    "x1=int('10',2)\n",
    "x2=x/x1\n",
    "x3=bin(x2)[2:]\n",
    "print \"(i) 110 / 10\"\n",
    "print \"  = binary\",x3\n",
    "print \"  = decimal\",x2\n",
    "x=int('1111',2)\n",
    "x1=int('110',2)\n",
    "x2=x/x1\n",
    "x3=bin(x2)[2:]\n",
    "print \"(ii) 1111 / 110\"\n",
    "print \"  = binary\",x3\n",
    "print \"  = decimal)\",x2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
