{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LEC20081124

# LEC20081124 - Introduction to Computer Programming CSC180...

This preview shows pages 1–7. Sign up to view the full content.

Introduction to Computer Programming November 24, 2008 CSC180 Fall 2008, University of Toronto

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Multiple view of data #include <stdio.h> int main() { typedef unsigned short WORD; typedef unsigned char BYTE; union { struct { WORD ax, bx, cx, dx; } word; struct { BYTE al, ah, bl, bh, cl, ch, dl, dh; } byte; } regs; regs.byte.al = 0x34; regs.byte.ah = 0x12; CSC180 Fall 2008, University of Toronto 1
printf("%x\n", regs.word.ax); regs.word.bx = 0xabcd; printf("%x %x\n", regs.byte.bl, regs.byte.bh); return 0; } CSC180 Fall 2008, University of Toronto 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Bit-fields A bit-field is a set of adjacent bits within a storage unit. A bit-filed is like a small integer. Example: struct file_date { unsigned int day : 5; unsigned int month : 4; unsigned int year : 7; }; Multiple view of data: CSC180 Fall 2008, University of Toronto 3
/* bitfield.c */ union U { float x; struct { unsigned int fraction : 23; unsigned int exponent : 8; unsigned int sign : 1; } y; }; int main() { union U data; /* hex: 40880000 */ data.x = 4.25; printf("%x %x %x\n", data.y.sign, data.y.exponent, data.y.fraction); /* hex: beba1170 */ data.y.sign = 0x1; data.y.exponent = 0x7d; data.y.fraction = 0x3a1170; CSC180 Fall 2008, University of Toronto 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
printf("%g\n", data.x); return 0; } Example:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}