summaryrefslogtreecommitdiff
path: root/Programming_With_C_by_Byron_S._Gottfried/chapter14.ipynb
blob: d89e7c723dcbe5a699ce5f37a04697895b9195e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 14: Low-Level Programming<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 14.14, Page number: 14.12<h3>"
     ]
    },
    {
     "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": [
      "<h3>Example 14.16, Page number: 14.13<h3>"
     ]
    },
    {
     "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": [
      "<h3>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": {}
  }
 ]
}