summaryrefslogtreecommitdiff
path: root/Let_us_C/chapter-14.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Let_us_C/chapter-14.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Let_us_C/chapter-14.ipynb')
-rw-r--r--Let_us_C/chapter-14.ipynb853
1 files changed, 853 insertions, 0 deletions
diff --git a/Let_us_C/chapter-14.ipynb b/Let_us_C/chapter-14.ipynb
new file mode 100644
index 00000000..becf792e
--- /dev/null
+++ b/Let_us_C/chapter-14.ipynb
@@ -0,0 +1,853 @@
+{
+ "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": [
+ "'''Program to print binary equivalent of integers using showbits( ) function'''\n",
+ "\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": [
+ "'''Program to shows one\u2019s complement operator in action.'''\n",
+ "\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": [
+ "'''Program to demonstrate file encryption utility'''\n",
+ "\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": [
+ "'''Program to demonstrate the effect of right shift operator.'''\n",
+ "\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": [
+ "'''Program to demonstrate the effect of left shift operator.'''\n",
+ "\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": [
+ "'''Program to decode the date field in directory entry using bitwise operators'''\n",
+ "\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": [
+ "'''Program to test whether a bit in a number is ON or OFF'''\n",
+ "\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": [
+ "'''Program to demonstrate that a number XORed with another number\n",
+ "twice gives the original number.'''\n",
+ "\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": {}
+ }
+ ]
+} \ No newline at end of file