C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture21_LowLevelPgm

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture21_LowLevelPgm

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 0 Lecture 23 Next Lecture – Chapter 15 - Writing Large Programs Topics for today – Low Level Coding – Real Time Programming – Embedded Systems 1 Low Level Programming • Usually machine dependent by nature – Storage unit differences and alignment issues – R to L, or L to R allocation of bits to units • Bit (byte) level manipulations are necessary for: – Operating systems, compilers, etc. – Encryption programs – Graphics – Embedded systems – others where fast execution or space utilization is critical • Portability is compromized unless the machine dependent computations are encapsulated – E.g. only use unsigned or signed variables • Lack of programmer documentation in this area is an absolute killer
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 2 Bit Level Operations • Performed on ints ( words ) and chars ( bytes ) in C • For example, on a sixteen bit value in which the bits are numbered from right to left 0 0 0 1 0 0 0 1 1 0 0 1 1 1 0 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0’s 0 0 0 1 0 0 0 1 1 0 0 1 1 1 0 0 Shift bits to the left 0’s 0 0 0 1 0 0 0 1 1 0 0 1 1 1 0 0 (or sign bits, if a signed value) Shift bits to the right 3 Bitwise Shifting • The bitwise shift operators are: << left shift >> right shift • The expression i << j represents the value of i shifted left j positions, zero-filled on the right. • The expression i >> j represents i shifted right j positions. – If i is of an unsigned type or if the value of i is not negative, then zero bits are added at the left as needed. – If i is negative, the result is machine dependent. Usually the sign bit is replicated • For portability, it’s best to perform shifts only on unsigned numbers. Examples: unsigned short int i, j; i = 13; /* i is now 13 (binary 0000000000001101) */ j = i << 2; /* j is now 52 (binary 0000000000110100) */ j = i >> 2; /* j is now 3 (binary 0000000000000011) */
Background image of page 2
4 Bitwise Operators • The other bitwise operators are: ~ bitwise complement ^ bitwise exclusive or | bitwise inclusive or after any promotion • Examples: unsigned short int i, j, k; i = 21; /* i is now 21 (binary 0000000000010101) */ j = 56; /* j is now 56 (binary 0000000000111000) */ k = ~i; /* k is now 65514 (binary 1111111111101010) */ k = i & j; /* k is now 16 (binary 0000000000010000) */ k = i ^ j; /* k is now 45 (binary 0000000000101101) */ k = i | j; /* k is now 61 (binary 0000000000111101) */ 5 Bitwise Operators (Continued) • Precedence of the other bitwise operators: Highest: ~ Lowest: | • Examples: • The precedence of &, ^, and | is lower than the precedence of the relational and equality operators. • Don’t use the shortcut assignment operators
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/15/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 20

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture21_LowLevelPgm

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online