{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 14: Low-Level Programming

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.14, Page number: 14.12

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a=0xf05a\n", "\n", "print \"%u\" %(a)\n", "print \"%x\" %(a>>6)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "61530\n", "3c1\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.16, Page number: 14.13

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "a=1\n", "nbits=16\n", "m=0x1<<(nbits-1)\n", "\n", "def main(a):\n", " mask=abs(m)\n", " for count in range(1,nbits+1):\n", " if a&mask:\n", " b=1\n", " else:\n", " b=0\n", "\n", " print \"%x\" %(b),\n", " if count%4==0:\n", " print \"\\t\",\n", "\n", " mask>>=1\n", " return\n", "print \"\\n1-->\\n\"\n", "main(1)\n", "print \"\\n-1-->\\n\"\n", "main(-1)\n", "print \"\\n0-->\\n\"\n", "main(0)\n", "print \"\\n2-->\\n\"\n", "main(2)\n", "print \"\\n-2-->\\n\"\n", "main(-2)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1-->\n", "\n", "0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t0 0 0 1 \t\n", "-1-->\n", "\n", "1 1 1 1 \t1 1 1 1 \t1 1 1 1 \t1 1 1 1 \t\n", "0-->\n", "\n", "0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t\n", "2-->\n", "\n", "0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t0 0 1 0 \t\n", "-2-->\n", "\n", "1 1 1 1 \t1 1 1 1 \t1 1 1 1 \t1 1 1 0 \t" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.23, Page Number: 14.20" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "string=c_char*50\n", "\n", "\n", "def convert(mm,dd,yy):\n", " yy-=1900\n", " ndays=long(30.42*(mm-1)+dd)\n", " if mm==2:\n", " ndays+=1\n", " if mm>2 and mm<8:\n", " ndays-=1\n", " if yy%4==0 and mm<2:\n", " ndays+=1\n", "\n", " ncycles=yy/4\n", " ndays+=ncycles*1461\n", "\n", " nyears=yy%4\n", " if nyears>0:\n", " ndays+=365*nyears+1\n", " if ndays>59:\n", " ndays-=1\n", "\n", " day=ndays%7\n", " yy+=1900\n", " if yy%4==0 and yy%400!=0:\n", " day+=1\n", " return day\n", "\n", "\n", "class date(Structure):\n", "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n", "\t\n", "\n", "class person(Structure):\n", "\t_fields_=[('name',string),('birthdate',date)]\n", "\t\t\n", "student=[]\n", "\n", "weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']\n", "\n", "month=['January','February','March','April','May','June','July','August',\\\n", " 'September','October','November','December']\n", "\n", "\n", "name='Rob Smith'\n", "birthdate=date(7,20,1972)\n", "student.append(person(name,birthdate))\n", "\n", "name='Judy Thompson'\n", "birthdate=date(11,27,1983)\n", "student.append(person(name,birthdate))\n", "\n", "name='Jim Williams'\n", "birthdate=date(12,29,1998)\n", "student.append(person(name,birthdate))\n", "\n", "name='Mort Davis'\n", "birthdate=date(6,10,2010)\n", "student.append(person(name,birthdate))\n", "\n", "\n", "\t\n", "for count in xrange(0,4):\n", " day_of_week=convert(student[count].birthdate.month,student[count].birthdate.day,student[count].birthdate.year)\n", " print student[count].name,\n", " print \" %s, %s %d %d \\n\" %(weekday[day_of_week],month[student[count].birthdate.month-1],student[count].birthdate.day,student[count].birthdate.year)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Rob Smith Thursday, July 20 1972 \n", "\n", "Judy Thompson Sunday, November 27 1983 \n", "\n", "Jim Williams Tuesday, December 29 1998 \n", "\n", "Mort Davis Thursday, June 10 2010 \n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }