{
 "metadata": {
  "name": "chapter-14.ipynb"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 14: Operations On Bits<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Binary Conversion, Page number: 483<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Function definition\n",
      "def showbits ( n ):\n",
      "    a = 15\n",
      "    for i in range(0,16):\n",
      "        andmask = 1 << a\n",
      "        k = n & andmask\n",
      "        if k == 0:\n",
      "            print  \"0\"\n",
      "        else:\n",
      "            print  \"1\" \n",
      "        a = a-1\n",
      "\n",
      "\n",
      "#Result\n",
      "for j in range(0,6):\n",
      "    print \"Decimal %d is same as binary \"%( j )\n",
      "    showbits ( j ) #function call\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal 0 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "Decimal 1 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "Decimal 2 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "Decimal 3 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "Decimal 4 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "Decimal 5 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>One's Complement, Page number: 484<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Function definition\n",
      "def showbits ( n ):\n",
      "    a = 15\n",
      "    for i in range(0,16):\n",
      "        andmask = 1 << a\n",
      "        k = n & andmask\n",
      "        if k == 0:\n",
      "            print  \"0\" \n",
      "        else:\n",
      "            print  \"1\" \n",
      "        a = a-1\n",
      "\n",
      "\n",
      "for j in range(0,4):\n",
      "    print  \"Decimal %d is same as binary \"%(j )\n",
      "    showbits ( j ) \n",
      "    k = ~j \n",
      "    print \"One\u2019s complement of %d is \"%( j ) \n",
      "    showbits ( k )"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal 0 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "One\u2019s complement of 0 is \n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "Decimal 1 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "One\u2019s complement of 1 is \n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "0\n",
        "Decimal 2 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "One\u2019s complement of 2 is \n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "Decimal 3 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "One\u2019s complement of 3 is \n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "1\n",
        "0\n",
        "0\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>File Encryption, Page number: 485<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Function definition\n",
      "def encrypt( ):\n",
      "    fs = open ( \"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #normal file \n",
      "    ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) # encrypted file \n",
      "    if ( not fs or not ft ):\n",
      "        print \"File opening error!\"  \n",
      "        exit()\n",
      "    while (1):\n",
      "        ch = fs.read(1)\n",
      "        if(not ch): #EOF\n",
      "            break\n",
      "        else:\n",
      "            ft.write(ascii(~ord(ch))) #complemented\n",
      "    #close files\n",
      "    fs.close()\n",
      "    ft.close()\n",
      "\n",
      "\n",
      "encrypt() #function call\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Right Shift Operator, Page number: 487<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Function definition\n",
      "def showbits ( n ):\n",
      "    a = 15\n",
      "    for i in range(0,16):\n",
      "        andmask = 1 << a\n",
      "        k = n & andmask\n",
      "        if k == 0:\n",
      "            print  \"0\" \n",
      "        else:\n",
      "            print  \"1\" \n",
      "        a = a-1\n",
      "\n",
      "#Variable declaration\n",
      "i = 5225\n",
      "\n",
      "print \"Decimal %d is same as binary \"%( i )\n",
      "showbits(i)#function call\n",
      "\n",
      "for j in range(0,6):\n",
      "    k = i >>j #right shift \n",
      "    print \"%d right shift %d gives \" %(i, j )\n",
      "    showbits ( k ) #function call "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal 5225 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "5225 right shift 0 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "5225 right shift 1 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "5225 right shift 2 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "5225 right shift 3 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "5225 right shift 4 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "5225 right shift 5 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Left Shift Operator, Page number: 488<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Function definition\n",
      "def showbits ( n ):\n",
      "    a = 15\n",
      "    for i in range(0,16):\n",
      "        andmask = 1 << a\n",
      "        k = n & andmask\n",
      "        if k == 0:\n",
      "            print  \"0\" \n",
      "        else:\n",
      "            print  \"1\" \n",
      "        a = a-1\n",
      "\n",
      "#Variable declaration\n",
      "i = 5225\n",
      "\n",
      "print  \"Decimal %d is same as binary \"%( i )\n",
      "showbits(i)#function call\n",
      "\n",
      "for j in range(0,6):\n",
      "    k = i <<j #left shift \n",
      "    print  \"%d left shift %d gives \" %(i, j )\n",
      "    showbits ( k ) #function call \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Decimal 5225 is same as binary \n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "5225 left shift 0 gives \n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "5225 left shift 1 gives \n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "5225 left shift 2 gives \n",
        "0\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "5225 left shift 3 gives \n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "5225 left shift 4 gives \n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "5225 left shift 5 gives \n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "1\n",
        "1\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "1\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n",
        "0\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Date Field in Directory, Page number: 492<h3>\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "d = 9\n",
      "m = 3\n",
      "y = 1990\n",
      "\n",
      "#Calculation and result\n",
      "date = ( y - 1980 ) * 512 + m * 32 + d \n",
      "print \"Date = %u\"%( date ) \n",
      "year = 1980 + ( date >> 9 ) \n",
      "month = ( (date << 7 ) >> 12 ) \n",
      "day = ( (date << 11 ) >> 11 ) \n",
      "print  \"Year =  \", year \n",
      "print  \"Month = %u\" %(m)\n",
      "print  \"Day = %u\"  %(d)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Date = 5225\n",
        "Year =   1990\n",
        "Month = 3\n",
        "Day = 9\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Bitwise AND Operator, Page number: 495<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "i = 65\n",
      "\n",
      "print \"value of i = \", i \n",
      "\n",
      "j = i & 32 #bitwise and\n",
      "if ( j == 0 ):\n",
      "    print  \"and its fifth bit is off\" \n",
      "else:\n",
      "    print \"and its fifth bit is on\"\n",
      "\n",
      "j = i & 64 #bitwise and\n",
      "if ( j == 0 ):\n",
      "    print \"whereas its sixth bit is off\" \n",
      "else:\n",
      "    print \"whereas its sixth bit is on\" \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "value of i =  65\n",
        "and its fifth bit is off\n",
        "whereas its sixth bit is on\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Bitwise XOR Operator, Page number: 500<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "b = 50\n",
      "\n",
      "#Calculation and result\n",
      "b = b ^ 12 \n",
      "print b  # this will print 62 \n",
      "b = b ^ 12 \n",
      "print  b  # this will print 50 \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "62\n",
        "50\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}