{ "metadata": { "name": "Chapter XII" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 12: Operations on bits" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 12.1, Page number: 282" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", "\n", " #Variable Declaration\n", " word1=77\n", " word2=150\n", " word3=210\n", " \n", " #Result\n", " print(word1&word2)\n", " print(word1&word1)\n", " print(word1&word2&word3)\n", " print(word1&1)\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4\n", "77\n", "0\n", "1\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 12.2, Page number: 286" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", "\n", " #variable Declarations\n", " w1=525\n", " w2=707\n", " w3=122\n", " \n", " print(\"{0:5} {1:5} {2:5}\".format(w1&w2,w1|w2,w1^w2))\n", " print(\"{0:5} {1:5} {2:5}\".format(~w1,~w2,~w3))\n", " print(\"{0:5} {1:5} {2:5}\".format(w1^w1,w1&~w2,w1|w2|w3))\n", " print(\"{0:5} {1:5}\".format(w2&w3,w1|w2&~w3))\n", " print(\"{0:5} {1:5}\".format(~(~w1&~w2),~(~w1|~w2)))\n", "\n", " #Exchange variables\n", " w1^=w2\n", " w2^=w1\n", " w1^=w2\n", " print(\"w1={0:3} w2={1:3}\".format(w1,w2))\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 513 719 206\n", " -526 -708 -123\n", " 0 12 767\n", " 66 653\n", " 719 513\n", "w1=707 w2=525\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 12.3, Page number: 288" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def shift(value,n):\n", " if(n>0):\n", " value <<=n #Left shift\n", " else:\n", " value >>=-n #Right Shift\n", "\n", " return value\n", "\n", "def main():\n", " #Variable Declaration\n", " w1=177777\n", " w2=444\n", "\n", " #Result\n", " print(\"{0:7} {1:7}\".format(shift(w1,5),w1<<5))\n", " print(\"{0:7} {1:7}\".format(shift(w1,-6),w1>>6))\n", " print(\"{0:7} {1:7}\".format(shift(w2,0),w2>>0))\n", " print(\"{0:7} \".format(shift(shift(w1,-3),3)))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5688864 5688864\n", " 2777 2777\n", " 444 444\n", " 177776 \n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 12.4, Page number: 290" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def rotate(value,n):\n", "\n", " #Calculation\n", " if(n>0):\n", " n=n%32\n", " else:\n", " n=-(-n%32)\n", "\n", " if(n==0):\n", " result=value\n", " elif(n>0):\n", " bits=value>>(32-n)\n", " result=value<>n|bits\n", "\n", " return result\n", "\n", "\n", "def main():\n", " w1=0xabcdef00\n", " w2=0xffff1122\n", "\n", " print(\"%x\"%rotate(w1,8))\n", " print(\"%x\"%rotate(w1,-16))\n", " print(\"%x\"%rotate(w2,4))\n", " print(\"%x\"%rotate(w2,-2))\n", " print(\"%x\"%rotate(w1,0))\n", " print(\"%x\"%rotate(w1,44))\n", " \n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "abcdef00ab\n", "abcdef00abcd\n", "ffff1122f\n", "3fffc448bfffc448\n", "abcdef00\n", "abcdef00abc\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }