{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 13 Number Systems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_2 PG-13.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Representation of the binary number 1101.101 in power of 2\n",
      "N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))+(0*2**(-2))+(1*2**(-3))=13.625\n",
      "\n",
      " The decimal equivalent of binary no 1101.101 is: 13.625\n"
     ]
    }
   ],
   "source": [
    "print \"Representation of the binary number 1101.101 in power of 2\"\n",
    "print \"N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))+(0*2**(-2))+(1*2**(-3))=13.625\"\n",
    "N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))+(0*2**(-2))+(1*2**(-3))\n",
    "print \"\\n The decimal equivalent of binary no 1101.101 is: %.3f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_3 PG-13.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "N=(5*8**2)+(6*8**1)+(7*8**0)=375\n",
      " Therefore decimal equivalent of 567 is: \n",
      "375\n"
     ]
    }
   ],
   "source": [
    "## print \"representation of the number 567 in power of 8\"\n",
    "print \"N=(5*8**2)+(6*8**1)+(7*8**0)=375\"\n",
    "print \" Therefore decimal equivalent of 567 is: \"\n",
    "N=(5*8**2)+(6*8**1)+(7*8**0)\n",
    "print \"%.0f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_4 PG-13.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Representation of the hexadecimalnumber 3FD in power of 16\n",
      "N=(3*16**2)+(F*16**1)+(D*16**0)=1021\n",
      " The Decimal equivalent of the Binary number 3FD is:\n",
      "1021\n"
     ]
    }
   ],
   "source": [
    "print \"Representation of the hexadecimalnumber 3FD in power of 16\"\n",
    "print \"N=(3*16**2)+(F*16**1)+(D*16**0)=1021\"\n",
    "print ' The Decimal equivalent of the Binary number 3FD is:'#\n",
    "N=(3*16**2)+(15*16**1)+(13*16**0)\n",
    "print \"%.0f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_5 PG-13.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Decimal equivalent of the number 231.23 with base 4 is: \n",
      "45.6875\n"
     ]
    }
   ],
   "source": [
    "print 'The Decimal equivalent of the number 231.23 with base 4 is: '\n",
    "x=(2*4**2)+(3*4**1)+(1*4**0)+(2*4**(-1))+(3*4**(-2))\n",
    "print \"%.4f\"%(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_6 PG-13.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "decimal from 0 to 9 in radix 5: \n",
      "     \t\t\t\t0=00\n",
      "     \t\t\t\t1=01\n",
      "     \t\t\t\t2=02\n",
      "     \t\t\t\t3=03\n",
      "     \t\t\t\t4=04\n",
      "     \t\t\t\t5=10\n",
      "     \t\t\t\t6=11\n",
      "     \t\t\t\t7=12\n",
      "     \t\t\t\t8=13\n",
      "     \t\t\t\t9=14\n"
     ]
    }
   ],
   "source": [
    "print \"decimal from 0 to 9 in radix 5: \"\n",
    "for i in range(0,10):\n",
    "    a=i/5#\n",
    "    b=(i%5)#\n",
    "    print \"     \\t\\t\\t\\t%d=%d%d\"%(i,a,b)##conversion from decimal to radix 5\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_7 PG-13.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of binary no 111101100 to octal equivalent is :\n",
      "0754\n"
     ]
    }
   ],
   "source": [
    "print \"Conversion of binary no 111101100 to octal equivalent is :\"\n",
    "a='111101100'#\n",
    "x=int(a,2)# decimal\n",
    "y=oct(x)# octal\n",
    "print \"%s\"%(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_8 PG-13.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of octal no 634 to binary equivalent is :\n",
      "110011100\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of octal no 634 to binary equivalent is :\"\n",
    "a='634'\n",
    "x=int(a,8)#first we convert to decimal\n",
    "y=bin(x)\n",
    "print y[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_9 PG-13.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of octal no 725.63 to binary equivalent\n",
      "\n",
      " The binary equivalent of the given octal number 725.63 is =111010102.100110\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"conversion of octal no 725.63 to binary equivalent\"\n",
    "a=725.63#\n",
    "#first we convert the number 725.63(octal)to decimal\n",
    "x=(7*8**2)+(2*8**1)+(5*8**0)+(6*8**(-1))+(3*8**(-2))#\n",
    "#then we convert the decimal to binary\n",
    "z=(x%1)\n",
    "x=floor(x)##separating the decimal from the integer part\n",
    "b=0#\n",
    "c=0#\n",
    "d=0#\n",
    "while(x>0) : #taking integer part into a matrix and convert to equivalent binary\n",
    "    y=(x%2)#\n",
    "    b=b+(10**c)*y#\n",
    "    x=x/2#\n",
    "    x=floor(x)#\n",
    "    c=c+1# \n",
    "\n",
    "for i in range(0,10):##converting the values after the decimal point into binary\n",
    "    z=z*2#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1:\n",
    "        z=z-1#\n",
    "    \n",
    "\n",
    "s=b+d#\n",
    "print \"\\n The binary equivalent of the given octal number 725.63 is =%.6f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_10 PG-13.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hexadecimal equivalent of 1101100010011011 binary is : \n",
      "d89b\n"
     ]
    }
   ],
   "source": [
    "print \"hexadecimal equivalent of 1101100010011011 binary is : \"\n",
    "a='1101100010011011'#\n",
    "x=int(a,2)#\n",
    "y=hex(x)#\n",
    "print y[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_11 PG-13.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Binary equivalent of 3FD hexadecimal is :\n",
      "001111111101\n"
     ]
    }
   ],
   "source": [
    "print \"Binary equivalent of 3FD hexadecimal is :\"\n",
    "a='3FD'\n",
    "y = bin(int(a, 16))[2:]\n",
    "print '00%s'%y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_13 PG-13.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of octal no 615 to its hexadecimal equivalent : 18d\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of octal no 615 to its hexadecimal equivalent :\",\n",
    "a='615'#\n",
    "x=int(a,8)\n",
    "y=hex(x)\n",
    "print y[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_14 PG-13.9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of hexadecimal no 25B to its octal equivalent :  133\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of hexadecimal no 25B to its octal equivalent : \",\n",
    "a='25B'#\n",
    "x=int(a,16)\n",
    "y=oct(x)\n",
    "print y[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_15 PG-13.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of binary no 1101.1 to its decimal equivalent =  13.5\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of binary no 1101.1 to its decimal equivalent =\",\n",
    "N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))#\n",
    "print \" %.1f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_16 PG-13.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of octal no 475.25 to its decimal equivalent =\n",
      " 317.32812\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of octal no 475.25 to its decimal equivalent =\"\n",
    "N=(4*8**2)+(7*8**1)+(5*8**0)+(2*8**(-1))+(5*8**(-2))#\n",
    "print \" %.5f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_17 PG-13.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of hexadecimal no 9B2.1A to its decimal equivalent =\n",
      " 2482.1\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of hexadecimal no 9B2.1A to its decimal equivalent =\"\n",
    "N=(9*16**2)+(11*16**1)+(2*16**0)+(1*16**(-1))+(10*16**(-2))#\n",
    "print \" %.1f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_18 PG-13.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of the number 3102.12 \n",
      " with base 4 to its decimal equivalent =\n",
      " 210.375\n"
     ]
    }
   ],
   "source": [
    "print \"Conversion of the number 3102.12 \\n with base 4 to its decimal equivalent =\"\n",
    "N=(3*4**3)+(1*4**2)+(0*4**1)+(2*4**0)+(1*4**(-1))+(2*4**(-2))#\n",
    "print \" %.3f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_19 PG-13.10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of the number 614.15 with base 7 to its decimal equivalent =\n",
      " 305.2449\n"
     ]
    }
   ],
   "source": [
    "print \"Conversion of the number 614.15 with base 7 to its decimal equivalent =\"\n",
    "N=(6*7**2)+(1*7**1)+(4*7**0)+(1*7**(-1))+(5*7**(-2))#\n",
    "print \" %.4f\"%(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_20 PG-13.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 37 to its binary equivalent = 100101\n"
     ]
    }
   ],
   "source": [
    "print \"Conversion of decimal number 37 to its binary equivalent =\",\n",
    "a=37#\n",
    "x=bin(a)#\n",
    "print \"%s\"%(x)[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_21 PG-13.12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of decimal number 214 to its octal equivalent = 26\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of decimal number 214 to its octal equivalent =\",\n",
    "a=214#\n",
    "x=oct(a)\n",
    "print x[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_22 PG-13.12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of decimal number 3509 to its hexadecimal equivalent = db5\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of decimal number 3509 to its hexadecimal equivalent =\",\n",
    "a=3509#\n",
    "x=hex(a)\n",
    "print x[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_23 PG-13.13 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conversion of decimal number 54 base  to a number with base 4 = 312\n"
     ]
    }
   ],
   "source": [
    "print \"conversion of decimal number 54 base  to a number with base 4 =\",\n",
    "a=54\n",
    "base = 4\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",
    "print base10toN(a,base)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_24 PG-13.13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 0.8125 base  to its binary equivalent \n",
      "\n",
      "The binary equivalent of the given decimal number 0.8125 is = 1.1010\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"Conversion of decimal number 0.8125 base  to its binary equivalent \"\n",
    "a=0.8125\n",
    "z=(a%1)#\n",
    "d=0#\n",
    "for i in range(0,10):##converting the values after the decimal point into binary\n",
    "    z=z*2#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1 :\n",
    "        z=z-1#\n",
    "    \n",
    "\n",
    "s=d#\n",
    "print \"\\nThe binary equivalent of the given decimal number 0.8125 is = %.4f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_25 PG-13.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 0.95 base  to its binary equivalent \n",
      "\n",
      " The binary equivalent of the given decimal number 0.95 is = 1.1110011\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"Conversion of decimal number 0.95 base  to its binary equivalent \"\n",
    "a=0.95#\n",
    "z=(a%1)#\n",
    "d=0#\n",
    "for i in range(0,10):##converting the values after the decimal point into binary\n",
    "    z=z*2#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1:\n",
    "        z=z-1#\n",
    "    \n",
    "\n",
    "s=d#\n",
    "print \"\\n The binary equivalent of the given decimal number 0.95 is = %.7f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_26 PG-13.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 0.640625 base  to its octal equivalent =\n",
      "\n",
      "The octal equivalent of the given decimal number 0.640625 is = 5.10\n"
     ]
    }
   ],
   "source": [
    "print \"Conversion of decimal number 0.640625 base  to its octal equivalent =\"\n",
    "a=0.640625\n",
    "z=(a%1)#\n",
    "d=0#\n",
    "for i in range(0,10):##converting the values after the decimal point into octal\n",
    "    z=z*8#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1:\n",
    "        z=z-q#\n",
    "    \n",
    "\n",
    "s=d#\n",
    "print \"\\nThe octal equivalent of the given decimal number 0.640625 is = %.2f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_27 PG-13.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 0.1289062 base  to its hexadecimal equivalent \n",
      "\n",
      "The hexadecimal equivalent of the given decimal number 0.640625 is = 2.167\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"Conversion of decimal number 0.1289062 base  to its hexadecimal equivalent \"\n",
    "a=0.1289062\n",
    "z=(a%1)#\n",
    "d=0#\n",
    "for i in range(0,10):##converting the values after the decimal point into octal\n",
    "    z=z*16#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1 :\n",
    "        z=z-q#\n",
    "    \n",
    "\n",
    "s=d#\n",
    "print \"\\nThe hexadecimal equivalent of the given decimal number 0.640625 is = %.3f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_28 PG-13.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 24.6 base  to its binary equivalent \n",
      "\n",
      "The binary equivalent of the given decimal number 24.6 is =11001.00110\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"Conversion of decimal number 24.6 base  to its binary equivalent \"\n",
    "a=24.6#\n",
    "z=(a%1)\n",
    "x=floor(a)##separating the decimal from the integer part\n",
    "b=0#\n",
    "c=0#\n",
    "d=0#\n",
    "while(x>0) : #taking integer part into a matrix and convert to equivalent binary\n",
    "    y=(x%2)#\n",
    "    b=b+(10**c)*y#\n",
    "    x=x/2#\n",
    "    x=floor(x)#\n",
    "    c=c+1#\n",
    "\n",
    "for i in range(0,10):##converting the values after the decimal point into binary\n",
    "    z=z*2#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    \n",
    "    if z>=1 :\n",
    "        z=z-1#\n",
    "    \n",
    "\n",
    "s=b+d#\n",
    "print \"\\nThe binary equivalent of the given decimal number 24.6 is =%.5f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_29 PG-13.15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conversion of decimal number 35.45 to its octal equivalent \n",
      "\n",
      "The octal equivalent of the given decimal number 35.45 is =326.46315\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"Conversion of decimal number 35.45 to its octal equivalent \"\n",
    "a=35.45#\n",
    "z=(a)\n",
    "x=floor(a)##separating the decimal from the integer part\n",
    "b=0#\n",
    "c=0#\n",
    "d=0#\n",
    "while(x>0):  #taking integer part into a matrix and convert to equivalent octal\n",
    "    y=(x%8)#\n",
    "    b=b+(10**c)*y#\n",
    "    x=x/8#\n",
    "    x=floor(x)#\n",
    "    c=c+1#\n",
    "\n",
    "for i in range(0,10):##converting the values after the decimal point into octal\n",
    "    z=z*8#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1 :\n",
    "        z=z-q#\n",
    "\n",
    "\n",
    "s=b+d#\n",
    "print \"\\nThe octal equivalent of the given decimal number 35.45 is =%.5f\"%(s)#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_32 Pg-18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "   Conversion of hexadecimal number 2AC5.D to \n",
      "\n",
      "\n",
      "   Decimal form = 10949.8125\n",
      "\n",
      "\n",
      "   Octal  form = 25311.40\n",
      "\n",
      "\n",
      "   Binary  form : \n",
      "\n",
      "       Integer part 2AC5 = 0010101011000101\n",
      "\n",
      "       Decimal part 0.D = 1.101000\n"
     ]
    }
   ],
   "source": [
    "from math import floor\n",
    "print \"\\n\\n   Conversion of hexadecimal number 2AC5.D to \\n\\n\"\n",
    "\n",
    "#conversion into decimal form\n",
    "N=(2*16**3)+(10*16**2)+(12*16**1)+(5*16**0)+(13*16**(-1))\n",
    "print \"   Decimal form = %.4f\\n\\n\"%(N)\n",
    "\n",
    "#conversion into octal form\n",
    "#we take the value of the decimal form and convert it to octal form\n",
    "z=(N%1)\n",
    "x=floor(N)##separating the decimal from the integer part\n",
    "b=0#\n",
    "c=0#\n",
    "d=0#\n",
    "while(x>0):  #taking integer part into a matrix and convert to equivalent binary\n",
    "    y=(x%8)#\n",
    "    b=b+(10**c)*y#\n",
    "    x=x/8#\n",
    "    x=floor(x)#\n",
    "    c=c+1#\n",
    "\n",
    "for i in range(0,10):##converting the values after the decimal point intooctal\n",
    "    z=z*8#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    if z>=1:\n",
    "        z=z-q#\n",
    "    \n",
    "\n",
    "s=b+d#\n",
    "print \"   Octal  form = %.2f\\n\\n\"%(s)#\n",
    "\n",
    "#conversion into binary form\n",
    "#we take the value of the decimal form and convert it to octal form\n",
    "z=(N%1)\n",
    "x=floor(N)##separating the decimal from the integer part\n",
    "b=0#\n",
    "c=0#\n",
    "d=0#\n",
    "while(x>0) : #taking integer part into a matrix and convert to equivalent binary\n",
    "    y=(x%2)#\n",
    "    b=b+(10**c)*y#\n",
    "    x=x/2#\n",
    "    x=floor(x)#\n",
    "    c=c+1#\n",
    "\n",
    "for i in range(0,10):##converting the values after the decimal point into binary\n",
    "    z=z*2#\n",
    "    q=floor(z)#\n",
    "    d=d+q/(10**i)#\n",
    "    \n",
    "    if z>=1 :\n",
    "        z=z-1#\n",
    "    \n",
    "\n",
    "s=b+d#\n",
    "print \"   Binary  form : \"\n",
    "print \"\\n       Integer part 2AC5 = 00%.0f\"%(b)\n",
    "print \"\\n       Decimal part 0.D = %.4f00\"%(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_33 Pg-18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    i)\n",
      "    Detemination of value of base x \n",
      "\n",
      "    (193)_x = (623)_8\n",
      "\n",
      "\n",
      "    First we convert (623)_8 octal into decimal\n",
      "\n",
      "\n",
      "          (623)_8 = (403)_10\n",
      "\n",
      "\n",
      "   Therefore (193)_x = (403)_10\n",
      "\n",
      "\n",
      "   (1*x**2)+(9*x**1)+(3*x**0) = 403\n",
      "\n",
      "\n",
      "    x**2 + 9x + 3 = 403\n",
      "\n",
      "\n",
      "    x**2 + 9x -400 = 0\n",
      "\n",
      "\n",
      "Therefore x1 = 16\n",
      "          x2 = -25\n",
      "\n",
      "   Negative is not applicable. Therefore x = 16\n",
      "\n",
      "   Hence (193)_16 = (623)_8\n",
      "\n",
      "\n",
      "\n",
      "    ii)\n",
      "    Detemination of value of base x \n",
      "\n",
      "    (225)_x = (341)_8\n",
      "\n",
      "\n",
      "    First we convert (341)_8 octal into decimal\n",
      "\n",
      "\n",
      "          (341)_8 = (225)_10\n",
      "\n",
      "\n",
      "Therefore (225)_x = (225)_10\n",
      "\n",
      "\n",
      "   (2*x**2)+(2*x**1)+(2*x**0) = 225\n",
      "\n",
      "\n",
      "    2x**2 + 2x +5 = 225\n",
      "\n",
      "\n",
      "    2x**2 + 2x -220= 0\n",
      "\n",
      "\n",
      "Therefore x1 = 10\n",
      "          x2 = -11\n",
      "\n",
      "   Negative is not applicable. Therefore x = 10\n",
      "\n",
      "   Hence (225)_10 = (341)_8\n",
      "\n",
      "\n",
      "\n",
      "    iii)\n",
      "    Detemination of value of base x \n",
      "\n",
      "    (211)_x = (152)_8\n",
      "\n",
      "\n",
      "    First we convert (152)_8 octal into decimal\n",
      "\n",
      "\n",
      "          (152)_8 = (106)_10\n",
      "\n",
      "\n",
      "Therefore (211)_x = (106)_10\n",
      "\n",
      "\n",
      "   (2*x**2)+(1*x**1)+(1*x**0) = 106\n",
      "\n",
      "\n",
      "    2x**2 + 1x +1 = 106\n",
      "\n",
      "\n",
      "    2x**2 + 1x -105 = 0\n",
      "\n",
      "\n",
      "Therefore x1 = 7\n",
      "          x2 = -8\n",
      "\n",
      "   Negative is not applicable. Therefore x = 7\n",
      "\n",
      "   Hence (211)_7 = (152)_8\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "print \"\\n    i)\\n    Detemination of value of base x \\n\"\n",
    "print \"    (193)_x = (623)_8\\n\\n\"\n",
    "print \"    First we convert (623)_8 octal into decimal\\n\\n\"\n",
    "N=(6*8**2)+(2*8**1)+(3*8**0)\n",
    "print \"          (623)_8 = (%.0f)_10\\n\\n\"%(N)\n",
    "print \"   Therefore (193)_x = (%.0f)_10\\n\\n\"%(N)\n",
    "print \"   (1*x**2)+(9*x**1)+(3*x**0) = %.0f\\n\\n\"%(N)\n",
    "print \"    x**2 + 9x + 3 = %.0f\\n\\n\"%(N)\n",
    "print \"    x**2 + 9x %.0f = 0\\n\\n\"%(3-N)\n",
    "a=1#\n",
    "b=9#\n",
    "c=-400\n",
    "x1=(-b+sqrt(b**2-4*a*c))/(2*a)#\n",
    "x2=(-b-sqrt(b**2-4*a*c))/(2*a)#\n",
    "print \"Therefore x1 = %.0f\\n          x2 = %.0f\"%(x1,x2)\n",
    "print \"\\n   Negative is not applicable. Therefore x = %.0f\"%(x1)\n",
    "print \"\\n   Hence (193)_%.0f = (623)_8\\n\\n\"%(x1)\n",
    "\n",
    "print \"\\n    ii)\\n    Detemination of value of base x \\n\"\n",
    "print \"    (225)_x = (341)_8\\n\\n\"\n",
    "print \"    First we convert (341)_8 octal into decimal\\n\\n\"\n",
    "N=(3*8**2)+(4*8**1)+(1*8**0)\n",
    "print \"          (341)_8 = (%.0f)_10\\n\\n\"%(N)\n",
    "print \"Therefore (225)_x = (%.0f)_10\\n\\n\"%(N)\n",
    "print \"   (2*x**2)+(2*x**1)+(2*x**0) = %.0f\\n\\n\"%(N)\n",
    "print \"    2x**2 + 2x +5 = %.0f\\n\\n\"%(N)\n",
    "print \"    2x**2 + 2x %.0f= 0\\n\\n\"%(5-N)\n",
    "a=2#\n",
    "b=2#\n",
    "c=-200\n",
    "x1=(-b+sqrt(b**2-4*a*c))/(2*a)#\n",
    "x2=(-b-sqrt(b**2-4*a*c))/(2*a)#\n",
    "print \"Therefore x1 = %.0f\\n          x2 = %.0f\"%(x1,x2)\n",
    "print \"\\n   Negative is not applicable. Therefore x = %.0f\"%(x1)\n",
    "print \"\\n   Hence (225)_%.0f = (341)_8\\n\\n\"%(x1)\n",
    "\n",
    "print \"\\n    iii)\\n    Detemination of value of base x \\n\"\n",
    "print \"    (211)_x = (152)_8\\n\\n\"\n",
    "print \"    First we convert (152)_8 octal into decimal\\n\\n\"\n",
    "N=(1*8**2)+(5*8**1)+(2*8**0)\n",
    "print \"          (152)_8 = (%.0f)_10\\n\\n\"%(N)\n",
    "print \"Therefore (211)_x = (%.0f)_10\\n\\n\"%(N)\n",
    "print \"   (2*x**2)+(1*x**1)+(1*x**0) = %.0f\\n\\n\"%(N)\n",
    "print \"    2x**2 + 1x +1 = %.0f\\n\\n\"%(N)\n",
    "print \"    2x**2 + 1x %.0f = 0\\n\\n\"%(1-N)\n",
    "a=2#\n",
    "b=1#\n",
    "c=-105\n",
    "x1=(-b+sqrt(b**2-4*a*c))/(2*a)#\n",
    "x2=(-b-sqrt(b**2-4*a*c))/(2*a)#\n",
    "print \"Therefore x1 = %.0f\\n          x2 = %.0f\"%(x1,x2)\n",
    "print \"\\n   Negative is not applicable. Therefore x = %.0f\"%(x1)\n",
    "print \"\\n   Hence (211)_%.0f = (152)_8\\n\\n\"%(x1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_34 Pg-20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  1's complement of 1101 =  0010\n"
     ]
    }
   ],
   "source": [
    "print \"  1's complement of 1101 = \",\n",
    "x='1101'\n",
    "\n",
    "def bitcmp(a):\n",
    "    a= str.replace(a,'0','x')\n",
    "    a = str.replace(a,'1','0')\n",
    "    a= str.replace(a,'x','1')\n",
    "    return a\n",
    "z = bitcmp(x)\n",
    "\n",
    "print \"%s\"%(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_35 Pg-20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1's complement of 10111001 =  01000110\n"
     ]
    }
   ],
   "source": [
    "print \"1's complement of 10111001 = \",\n",
    "x='10111001'\n",
    "def bitcmp(a):\n",
    "    a= str.replace(a,'0','x')\n",
    "    a = str.replace(a,'1','0')\n",
    "    a= str.replace(a,'x','1')\n",
    "    return a\n",
    "z = bitcmp(x)\n",
    "\n",
    "print \"%s\"%(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_36 Pg-21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  2's complement 1001 is :  0111\n"
     ]
    }
   ],
   "source": [
    "print \"  2's complement 1001 is : \",\n",
    "x='1001'#\n",
    "def bitcmp(a):\n",
    "    a= str.replace(a,'0','x')\n",
    "    a = str.replace(a,'1','0')\n",
    "    a= str.replace(a,'x','1')\n",
    "    return a\n",
    "z = bitcmp(x)\n",
    "z = int(z,2)+1\n",
    "z = bin(z)\n",
    "print \"0%s\"%(z[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_37 Pg-21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2's complement 10100011 is :  01011101\n"
     ]
    }
   ],
   "source": [
    "print \"2's complement 10100011 is : \",\n",
    "x='10100011'\n",
    "z = bitcmp(x)\n",
    "z = int(z,2)+1\n",
    "z = bin(z)\n",
    "print \"0%s\"%(z[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_38 Pg-22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7's complement of(612)_8 is :  0165\n"
     ]
    }
   ],
   "source": [
    "print \"7's complement of(612)_8 is : \",\n",
    "x='612'\n",
    "x=int(x,8)\n",
    "x = bin(x)\n",
    "x = bitcmp(x)[2:]\n",
    "x = int(x,2)\n",
    "x = oct(x)\n",
    "print x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_40 Pg-22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15's complement (A9B)_16 is : 564\n"
     ]
    }
   ],
   "source": [
    "print \"15's complement (A9B)_16 is :\",\n",
    "x='A9B'#\n",
    "def hex15cmp(n):\n",
    "    nn = int(n,16)\n",
    "    nn = bin(nn)\n",
    "    nn = bitcmp(nn)[2:]\n",
    "    nn = int(nn,2)\n",
    "    nn = hex(nn)\n",
    "    return nn[2:]\n",
    "\n",
    "y=hex15cmp(x)##hexadecimal to decimal conversion#\n",
    "print \"%s\"%y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_41 Pg-23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16's complement (A8C)_16 is :  574\n"
     ]
    }
   ],
   "source": [
    "print \"16's complement (A8C)_16 is : \",\n",
    "x='A8C'#\n",
    "y=hex15cmp(x)##hexadecimal 15's comp\n",
    "y = hex(int(y,16)+1)[2:]\n",
    "print \"%s\"%(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_42 Pg-23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the addition of given numbers is:  1101\n"
     ]
    }
   ],
   "source": [
    "x='1010'#\n",
    "y='0011'#\n",
    "#binary to decimal conversion#\n",
    "x=int(x,2)\n",
    "y=int(y,2)\n",
    "z=x+y#\n",
    "a=bin(z)[2:]#decimal to binary conversion#\n",
    "print 'the addition of given numbers is: ',\n",
    "print \"%s\"%(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 13_43 Pg-23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the addition of given numbers in binary is:  101011\n",
      "\n",
      " the addition of given numbers in decimal is:  43\n"
     ]
    }
   ],
   "source": [
    "x=28#\n",
    "y=15#\n",
    "# decimal to binary  conversion#\n",
    "z=x+y#\n",
    "a=bin(z)\n",
    "print 'the addition of given numbers in binary is: ',\n",
    "print \"%s\"%(a)[2:]\n",
    "print '\\n the addition of given numbers in decimal is: ',\n",
    "print \"%.0f\"%(z)"
   ]
  }
 ],
 "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
}
