summaryrefslogtreecommitdiff
path: root/ANSI_C_Programming/chapter14.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'ANSI_C_Programming/chapter14.ipynb')
-rw-r--r--ANSI_C_Programming/chapter14.ipynb407
1 files changed, 407 insertions, 0 deletions
diff --git a/ANSI_C_Programming/chapter14.ipynb b/ANSI_C_Programming/chapter14.ipynb
new file mode 100644
index 00000000..c29af0ac
--- /dev/null
+++ b/ANSI_C_Programming/chapter14.ipynb
@@ -0,0 +1,407 @@
+{
+ "metadata": {
+ "name": "chapter14.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 14: OPERATIONS ON BITS"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:449"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Print binary equivalent of integers using showbits() function()\n",
+ "def showbits(x):\n",
+ " return bin(x)[2:].zfill(16)\n",
+ "for j in range(0,6,1):\n",
+ " print \"Decimal %d is same as binary\" % (j)\n",
+ " print showbits(j)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 0 is same as binary\n",
+ "0000000000000000\n",
+ "Decimal 1 is same as binary\n",
+ "0000000000000001\n",
+ "Decimal 2 is same as binary\n",
+ "0000000000000010\n",
+ "Decimal 3 is same as binary\n",
+ "0000000000000011\n",
+ "Decimal 4 is same as binary\n",
+ "0000000000000100\n",
+ "Decimal 5 is same as binary\n",
+ "0000000000000101\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:450"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of One's complement operator\n",
+ "def showbits(x):\n",
+ " return bin(x)[2:].zfill(16)\n",
+ "for j in range(0,4,1):\n",
+ " print \"Decimal %d is same as binary\" % (j)\n",
+ " print showbits(j)\n",
+ " k=j^65535 #using xor for making one's complement\n",
+ " print \"One's complement of %d is\" % (j)\n",
+ " print showbits(k)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 0 is same as binary\n",
+ "0000000000000000\n",
+ "One's complement of 0 is\n",
+ "1111111111111111\n",
+ "Decimal 1 is same as binary\n",
+ "0000000000000001\n",
+ "One's complement of 1 is\n",
+ "1111111111111110\n",
+ "Decimal 2 is same as binary\n",
+ "0000000000000010\n",
+ "One's complement of 2 is\n",
+ "1111111111111101\n",
+ "Decimal 3 is same as binary\n",
+ "0000000000000011\n",
+ "One's complement of 3 is\n",
+ "1111111111111100\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:451"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#file encryption utility\n",
+ "import sys\n",
+ "def encrypt():\n",
+ " try:\n",
+ " fs=open('SOURCE.txt','r') #normal file\n",
+ " ft=open('TARGET.txt','w') #encrypted file\n",
+ " except:\n",
+ " print \"File opening error!\"\n",
+ " sys.exit(1)\n",
+ " while True:\n",
+ " ch=fs.read(1)\n",
+ " if not ch:\n",
+ " break\n",
+ " ft.write(bytearray([ord(ch)^0xff]))\n",
+ " fs.close()\n",
+ " ft.close()\n",
+ "encrypt()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:452"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of Right shift operator\n",
+ "def showbits(x):\n",
+ " return bin(x)[2:].zfill(16)\n",
+ "i=5225\n",
+ "print \"Decimal %d is same as binary\" % (i)\n",
+ "print showbits(i)\n",
+ "for j in range(0,6,1):\n",
+ " k=i>>j #right shift operator\n",
+ " print \"%d right shift %d gives\" % (i,j)\n",
+ " print showbits(k)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 5225 is same as binary\n",
+ "0001010001101001\n",
+ "5225 right shift 0 gives\n",
+ "0001010001101001\n",
+ "5225 right shift 1 gives\n",
+ "0000101000110100\n",
+ "5225 right shift 2 gives\n",
+ "0000010100011010\n",
+ "5225 right shift 3 gives\n",
+ "0000001010001101\n",
+ "5225 right shift 4 gives\n",
+ "0000000101000110\n",
+ "5225 right shift 5 gives\n",
+ "0000000010100011\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:453-454"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of left shift operator\n",
+ "def showbits(x):\n",
+ " return bin(x)[2:].zfill(16)\n",
+ "i=5225\n",
+ "print \"Decimal %d is same as binary\" % (i)\n",
+ "print showbits(i)\n",
+ "for j in range(0,5,1):\n",
+ " mask = 2 ** 16 - 1\n",
+ " k = (i << j) & mask #left shift operator\n",
+ " print \"%d right shift %d gives\" % (i,j)\n",
+ " print showbits(k)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Decimal 5225 is same as binary\n",
+ "0001010001101001\n",
+ "5225 right shift 0 gives\n",
+ "0001010001101001\n",
+ "5225 right shift 1 gives\n",
+ "0010100011010010\n",
+ "5225 right shift 2 gives\n",
+ "0101000110100100\n",
+ "5225 right shift 3 gives\n",
+ "1010001101001000\n",
+ "5225 right shift 4 gives\n",
+ "0100011010010000\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:457-458"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Decoding date field in directory entry using bitwise operators\n",
+ "#Note:Answer in the book is wrong\n",
+ "d=9\n",
+ "m=3\n",
+ "y=1990\n",
+ "date=(y-1980)*512+m*32+d\n",
+ "print \"Date=%u\\n\" % (date)\n",
+ "year=1980+(date>>9)\n",
+ "month=((date<<7)>>12)\n",
+ "day=((date<<11)>>11)\n",
+ "print \"Year=%u\" % (year)\n",
+ "print \"Month=%u\" % (month)\n",
+ "print \"Day=%u\" % (day)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date=5225\n",
+ "\n",
+ "Year=1990\n",
+ "Month=163\n",
+ "Day=5225\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:460"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#To test whether a bit in a number is ON or OFF\n",
+ "i=65\n",
+ "print \"Value of i=%d\\n\" % (i)\n",
+ "j=i&32\n",
+ "if j==0:\n",
+ " print \"and its fifth bit is off\\n\"\n",
+ "else:\n",
+ " print \"and its fifth bit is on\\n\"\n",
+ "j=i&64\n",
+ "if j==0:\n",
+ " print \"whereas its sixth bit is off\\n\"\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",
+ "\n",
+ "and its fifth bit is off\n",
+ "\n",
+ "whereas its sixth bit is on\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:463"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of XOR operator\n",
+ "b=50\n",
+ "b=b^12 #XOR operator\n",
+ "print \"%d\\n\" % (b) #this will print 62\n",
+ "b=b^12\n",
+ "print \"%d\\n\" % (b) #this will print 50"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "62\n",
+ "\n",
+ "50\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:463-464"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#function for 16 bit binary representation of a number\n",
+ "def showbit(n):\n",
+ " for i in range(15,-1,-1):\n",
+ " andmask=1<<i\n",
+ " k=n&andmask\n",
+ " if k==0:\n",
+ " print 0, \n",
+ " else:\n",
+ " print 1, \n",
+ " print \"\\n\"\n",
+ "showbit(32)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file