diff options
Diffstat (limited to 'Physics_for_BSc(Paper-3)/Chapter11.ipynb')
-rwxr-xr-x | Physics_for_BSc(Paper-3)/Chapter11.ipynb | 1412 |
1 files changed, 1412 insertions, 0 deletions
diff --git a/Physics_for_BSc(Paper-3)/Chapter11.ipynb b/Physics_for_BSc(Paper-3)/Chapter11.ipynb new file mode 100755 index 00000000..cdfa523b --- /dev/null +++ b/Physics_for_BSc(Paper-3)/Chapter11.ipynb @@ -0,0 +1,1412 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:3823c7883d62d9ae64fea382b9f92645401f2140b77356ae18d5124fb244bdec"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "11: Digital principles"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.1, Page number 408"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1001101'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "b=int(a,2); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 77\n"
+ ]
+ }
+ ],
+ "prompt_number": 136
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.2, Page number 408"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1001.101'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "def parse_bin(s):\n",
+ " t = s.split('.')\n",
+ " return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])\n",
+ "\n",
+ "n=parse_bin(a); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 9.625\n"
+ ]
+ }
+ ],
+ "prompt_number": 137
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.3, Page number 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='246'; #octal number\n",
+ "\n",
+ "#Calculation\n",
+ "n=int(a,8); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 166\n"
+ ]
+ }
+ ],
+ "prompt_number": 138
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.4, Page number 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='136.24'; #octal number\n",
+ "\n",
+ "#Calculation\n",
+ "def parse_bin(s):\n",
+ " t = s.split('.')\n",
+ " return int(t[0], 8) + int(t[1], 8) / 8.**len(t[1])\n",
+ "\n",
+ "n=parse_bin(a); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 94.3125\n"
+ ]
+ }
+ ],
+ "prompt_number": 139
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.5, Page number 410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='4FE'; #hexadecimal number\n",
+ "\n",
+ "#Calculation\n",
+ "n=int(a,16); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 1278\n"
+ ]
+ }
+ ],
+ "prompt_number": 140
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.6, Page number 411"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='10'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "b=int(a,2); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 141
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.7, Page number 411"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='11111'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "b=int(a,2); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 31\n"
+ ]
+ }
+ ],
+ "prompt_number": 142
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.8, Page number 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=41; #decimal number\n",
+ "\n",
+ "#Calculation\n",
+ "n=bin(a); #binary equivalent\n",
+ "\n",
+ "#Result\n",
+ "print \"binary equivalent is\",n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "binary equivalent is 0b101001\n"
+ ]
+ }
+ ],
+ "prompt_number": 143
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.9, Page number 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=0.65625; #decimal number\n",
+ "\n",
+ "#Calculation\n",
+ "x1=a*2;\n",
+ "n1=int(x1); #carry\n",
+ "x2=(x1-n1)*2;\n",
+ "n2=int(x2); #carry\n",
+ "x3=x2*2;\n",
+ "n3=int(x3); #carry\n",
+ "x4=(x3-n3)*2;\n",
+ "n4=int(x4); #carry\n",
+ "x5=x4*2;\n",
+ "n5=int(x5); #carry\n",
+ "\n",
+ "#Result\n",
+ "print \"since the given decimal number is fractional the binary equivalent will be 0.\",n1,n2,n3,n4,n5"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "since the given decimal number is fractional the binary equivalent will be 0. 1 0 1 0 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 144
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.10, Page number 413"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='110111.101'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "def parse_bin(s):\n",
+ " t = s.split('.')\n",
+ " return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])\n",
+ "\n",
+ "n=parse_bin(a); #number in decimal form\n",
+ "\n",
+ "#Result\n",
+ "print \"number in decimal form is\",n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "number in decimal form is 55.625\n"
+ ]
+ }
+ ],
+ "prompt_number": 145
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.11, Page number 413"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1001'; #binary number\n",
+ "b='1111'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "sum=int(a,2)+int(b,2); #sum of two binary numbers\n",
+ "sum=bin(sum);\n",
+ "\n",
+ "#Result\n",
+ "print \"sum of two binary numbers is\",sum"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "sum of two binary numbers is 0b11000\n"
+ ]
+ }
+ ],
+ "prompt_number": 146
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.12, Page number 414"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='011'; #binary number\n",
+ "b='101'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "diff=int(b,2)-int(a,2); #difference of two binary numbers\n",
+ "diff=bin(diff);\n",
+ "\n",
+ "#Result\n",
+ "print \"difference of two binary numbers is\",diff"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference of two binary numbers is 0b10\n"
+ ]
+ }
+ ],
+ "prompt_number": 147
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.13, Page number 415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1011'; #binary number\n",
+ "b='0110'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "diff=int(a,2)-int(b,2); #difference of two binary numbers\n",
+ "diff=bin(diff);\n",
+ "\n",
+ "#Result\n",
+ "print \"difference of two binary numbers is\",diff"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference of two binary numbers is 0b101\n"
+ ]
+ }
+ ],
+ "prompt_number": 148
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.14, Page number 415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "n='010' #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "def complement(n):\n",
+ " size = len(format(n, 'b'))\n",
+ " comp = n ^ ((1 << size) - 1)\n",
+ " return '0b1{0:0{1}b}'.format(comp, size)\n",
+ "\n",
+ "a=complement((0b010))\n",
+ "\n",
+ "#Result\n",
+ "print \"1's complement of the number is\",a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1's complement of the number is 0b101\n"
+ ]
+ }
+ ],
+ "prompt_number": 149
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.15, Page number 415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "n='1110' #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "def complement(n):\n",
+ " size = len(format(n, 'b'))\n",
+ " comp = n ^ ((1 << size) - 1)\n",
+ " return '0b{0:0{1}b}'.format(comp, size)\n",
+ "\n",
+ "a=complement((0b1110))\n",
+ "\n",
+ "#Result\n",
+ "print \"1's complement of the number is\",a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1's complement of the number is 0b0001\n"
+ ]
+ }
+ ],
+ "prompt_number": 150
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.16, Page number 416"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1011'; #binary number\n",
+ "b='1101'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "diff=int(b,2)-int(a,2); #difference of two binary numbers\n",
+ "diff=bin(diff);\n",
+ "\n",
+ "#Result\n",
+ "print \"difference of two binary numbers is\",diff"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference of two binary numbers is 0b10\n"
+ ]
+ }
+ ],
+ "prompt_number": 152
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.17, Page number 416"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1100'; #binary number\n",
+ "b='1000'; #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "diff=int(b,2)-int(a,2); #difference of two binary numbers\n",
+ "diff=bin(diff);\n",
+ "\n",
+ "#Result\n",
+ "print \"difference of two binary numbers is\",diff"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference of two binary numbers is -0b100\n"
+ ]
+ }
+ ],
+ "prompt_number": 153
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.18, Page number 416"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "n='0101' #binary number\n",
+ "b='1'\n",
+ "\n",
+ "#Calculation\n",
+ "def complement(n):\n",
+ " size = len(format(n, 'b'))\n",
+ " comp = n ^ ((1 << size) - 1)\n",
+ " return '0b1{0:0{1}b}'.format(comp, size)\n",
+ "\n",
+ "a=complement((0b0101))\n",
+ "ans=int(a,2)+int(b,2); #2's complement of number\n",
+ "\n",
+ "#Result\n",
+ "print \"2's complement of number is\",bin(ans)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2's complement of number is 0b1011\n"
+ ]
+ }
+ ],
+ "prompt_number": 154
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.19, Page number 417"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "n='101100' #binary number\n",
+ "b='1'\n",
+ "\n",
+ "#Calculation\n",
+ "def complement(n):\n",
+ " size = len(format(n, 'b'))\n",
+ " comp = n ^ ((1 << size) - 1)\n",
+ " return '0b{0:0{1}b}'.format(comp, size)\n",
+ "\n",
+ "a=complement((0b101100))\n",
+ "ans=int(a,2)+int(b,2); #2's complement of number\n",
+ "\n",
+ "#Result\n",
+ "print \"2's complement of number is\",bin(ans)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2's complement of number is 0b10100\n"
+ ]
+ }
+ ],
+ "prompt_number": 155
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.20, Page number 417"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=bin(6);\n",
+ "b=bin(2);\n",
+ "\n",
+ "#Calculation\n",
+ "c= int(a,2)-int(b,2); #difference\n",
+ "\n",
+ "#Result\n",
+ "print \"difference is\",bin(c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference is 0b100\n"
+ ]
+ }
+ ],
+ "prompt_number": 156
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.21, Page number 418"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=bin(3);\n",
+ "b=bin(5);\n",
+ "\n",
+ "#Calculation\n",
+ "c=int(a,2)-int(b,2); #difference\n",
+ "\n",
+ "#Result\n",
+ "print \"difference is\",bin(c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference is -0b10\n"
+ ]
+ }
+ ],
+ "prompt_number": 157
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.22, Page number 420"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='01101110' #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=hex(int(a,2)); #equivalent hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x6e\n"
+ ]
+ }
+ ],
+ "prompt_number": 158
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.23, Page number 420"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='10110101111011' #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=hex(int(a,2)); #equivalent hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x2d7b\n"
+ ]
+ }
+ ],
+ "prompt_number": 159
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.24, Page number 421"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='1100111111011' #binary number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=hex(int(a,2)); #equivalent hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x19fb\n"
+ ]
+ }
+ ],
+ "prompt_number": 160
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.25, Page number 421"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='11001110001' #integral part of binary number\n",
+ "b='000101111001'; #fractional part of binary number\n",
+ "\n",
+ "#Calculation\n",
+ "c=hex(int(a,2)); #converting into hexadecimal number\n",
+ "d=hex(int(b,2)); #converting into hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",c,\".\",d"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x671 . 0x179\n"
+ ]
+ }
+ ],
+ "prompt_number": 161
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.26, Page number 421"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='6B9'; #umber in hexadecimal\n",
+ "\n",
+ "#Calculation\n",
+ "def float_to_binary(num):\n",
+ " exponent=0\n",
+ " shifted_num=num\n",
+ " while shifted_num != int(shifted_num): \n",
+ " shifted_num*=2\n",
+ " exponent+=1\n",
+ " if exponent==0:\n",
+ " return '{0:0b}'.format(int(shifted_num))\n",
+ " binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)\n",
+ " integer_part=binary[:-exponent]\n",
+ " fractional_part=binary[-exponent:].rstrip('0')\n",
+ " return '{0}.{1}'.format(integer_part,fractional_part)\n",
+ "\n",
+ "def floathex_to_binary(floathex):\n",
+ " num = float.fromhex(floathex)\n",
+ " return float_to_binary(num)\n",
+ "\n",
+ "ans=(floathex_to_binary('6B9')); #equivalent binary number \n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent binary number is\",ans "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent binary number is 11010111001\n"
+ ]
+ }
+ ],
+ "prompt_number": 162
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.27, Page number 421"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='6D.3A'; #number in hexadecimal\n",
+ "\n",
+ "#Calculation\n",
+ "def float_to_binary(num):\n",
+ " exponent=0\n",
+ " shifted_num=num\n",
+ " while shifted_num != int(shifted_num): \n",
+ " shifted_num*=2\n",
+ " exponent+=1\n",
+ " if exponent==0:\n",
+ " return '{0:0b}'.format(int(shifted_num))\n",
+ " binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)\n",
+ " integer_part=binary[:-exponent]\n",
+ " fractional_part=binary[-exponent:].rstrip('0')\n",
+ " return '{0}.{1}'.format(integer_part,fractional_part)\n",
+ "\n",
+ "def floathex_to_binary(floathex):\n",
+ " num = float.fromhex(floathex)\n",
+ " return float_to_binary(num)\n",
+ "\n",
+ "ans=(floathex_to_binary('6D.3A')); #equivalent binary number \n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent binary number is\",ans "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent binary number is 1101101.0011101\n"
+ ]
+ }
+ ],
+ "prompt_number": 163
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.28, Page number 422"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=5000; #decimal number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=hex(a); #equivalent hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x1388\n"
+ ]
+ }
+ ],
+ "prompt_number": 164
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.29, Page number 422"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=14619; #decimal number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=hex(a); #equivalent hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x391b\n"
+ ]
+ }
+ ],
+ "prompt_number": 165
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.30, Page number 422"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=15101; #decimal number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=hex(a); #equivalent hexadecimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent hexadecimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent hexadecimal number is 0x3afd\n"
+ ]
+ }
+ ],
+ "prompt_number": 166
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.31, Page number 423"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='4B8'; #hexadecimal number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=int(a,16); #equivalent decimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent decimal number is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent decimal number is 1208\n"
+ ]
+ }
+ ],
+ "prompt_number": 167
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.32, Page number 423"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='3A4B'; #hexadecimal number\n",
+ "\n",
+ "#Calculation\n",
+ "ans=int(a,16); #equivalent decimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent decimal number is\",ans\n",
+ "print \"answer given in the book is wrong\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent decimal number is 14923\n",
+ "answer given in the book is wrong\n"
+ ]
+ }
+ ],
+ "prompt_number": 168
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.33, Page number 424"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='2B6D'; #number in hexadecimal\n",
+ "\n",
+ "#Calculation\n",
+ "ans=int(a,16); #equivalent decimal number\n",
+ "\n",
+ "#Result\n",
+ "print \"equivalent decimal number is\",ans "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent decimal number is 11117\n"
+ ]
+ }
+ ],
+ "prompt_number": 169
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example number 11.34, Page number 424"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#importing modules\n",
+ "import math\n",
+ "from __future__ import division\n",
+ "\n",
+ "#Variable declaration\n",
+ "a='FC2'; #number in hexadecimal\n",
+ "b='DAB'; #number in hexadecimal\n",
+ "\n",
+ "#Calculation\n",
+ "ans=int(a,16)-int(b,16); #difference of two numbers\n",
+ "ans=hex(ans);\n",
+ "\n",
+ "#Result\n",
+ "print \"difference of two numbers is\",ans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "difference of two numbers is 0x217\n"
+ ]
+ }
+ ],
+ "prompt_number": 170
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |