K&R Chapter 2
Types, Operators, and
Expressions
Dr. Charles R. Severance
www.cc4e.com
code.cc4e.com (sample code)
online.dr-chuck.com
Chapter 2 – Unique Areas
• Section 2.2 – Data types and storage allocation – character, integer,
short, long get all the attention. Interestingly float and double are
pretty consistent with modern languages
• Section 2.7 – Type conversion – integer division and Python 2.0 and
3.0 pain and history
• Section 2.9 – Bitwise Logical Operations – Rarely used – In Chapter 6
struct data types are better at extracting bit ranges
Ah, Division…
Can it be that it was all so simple then?
Or has time rewritten every line?
If we had the chance to do it all again, tell me, would we, could we?
From the film "The Way We Were" (1973)
Python 2 to Python 3
• Python 2 was very popular – but under attack from modern languages
• Strings were ASCII (Not Unicode)
• I/O (print) was part of the language – limited evolution
• Converting took from 2008 – 2020
• Programmers got a lot of help
• An automated code convertor
• Back-porting of future library code to support 2.x ad 3.x at the same time
• Division of two integers
• Python 2 – Returns an integer and the division is truncated (3/4 == 0)
• Python 3 – Any division converts operands to float and produces a float (3/4 == 0.75)
• Python 2 division truncated because C integer division truncated (3/4 == 0)
Integer Division in C
• Less of a problem because C is a (pretty) strongly typed language
• Python
• x=3/4
• The type of x is part of x and when x is used many lines later, perhaps in a
library routine, it all gets figured out (often badly)
• C must declare “x” as float, double, int, etc. So when a C programmer
writes a division, they know they need to cast values or use float
constants to trigger type conversion in expressions
• float x;
• x = ((float) 3 / 4);
• x = x * 2.5;
A Quick Tutorial on
Number Bases
Place Value – Base 10
1
Tens place One’s place
2
42 3 4x10=40
4
1
2 2
4x101 + 2x100 = 40 + 2 = 42
Place Value – Base 8
1
Eights place One’s place
2
52 3 5x8=40
4
5
1
2 2
5x81 + 2x80 = 40 + 2 = 42
1
Base 16 2
0 1 2 3 4 5 6 7 8 9 A B C D E F
Sixteens place One’s place
1
2A 2 16x2=32
3
4
5
A (10)
6
7
8
9
A
2x161 + 10x100 = 32 + 10 = 42
num = int(input('Enter a base-10 number: '))
out = '' $ python3 kr_02_01.py
val = num Enter a base-10 number: 1234
while (val > 0 ) : 1234 % 8 = 2
digit = val % 8 1234 / 8 = 154
print(val,'% 8 = ',digit) 154 % 8 = 2
out = str(digit) + out; 154 / 8 = 19
new = int(val / 8) 19 % 8 = 3
print(val,'/ 8 = ',new) 19 / 8 = 2
val = new 2 % 8 = 2
print(num, 'in base-8 is', out) 2 / 8 = 0
1234 in base-8 is 2322
digits = '0123456789abcdef' 1234 % 16 = 2
out = '' 1234 / 16 = 77
val = num 77 % 16 = 13
while (val > 0 ) : 77 / 16 = 4
digit = val % 16 4 % 16 = 4
print(val,'% 16 = ',digit) 4 / 16 = 0
out = digits[digit:digit+1] + out; 1234 in base-16 is 4d2
new = int(val / 16)
print(val,'/ 16 = ',new)
val = new
print(num, 'in base-16 is', out)
kr_02_01.py
ASCII
• American Standard
Code for Information
Interchange
Upper case H in ASCII is
72 in base 10
0x48 in base-16
00100100 in base-2
https://en.wikipedia.org/wiki/ASCII
http://www.catonmat.net/download/ascii-cheat-sheet.png
C and UNIX, Byte-Addressable
Computers
• In the 1970’s computers were getting smaller, cheaper, and moving
away from (8 bit) characters being packed into (32 bit) integer words
with masking and shifting and towards direct byte addressing in
assembly language
• A major reason to move UNIX from the “B” language to the “C”
language was to have a language that made efficient use of byte
addressing
CDC-6500 Character Support
• The CDC-6500 had 60 bit words and 6-bit upper case characters
• https://en.wikipedia.org/wiki/CDC_display_code
H E L L O W O R L H E L L O W O R L
D - - - - - - - - -
H E L L W O R L Bit Mask
O After Mask
O Bit Shift
Thanks Ken, Brian, and Dennis!
• Thanks to the pioneering work in Dealing with data of different endianness is sometimes termed
the NUXI problem. This terminology alludes to the byte order
the early 1970’s, modern conflicts encountered while adapting UNIX, which ran on the
mixed-endian PDP-11, to a big-endian IBM Series/1 computer.
programmers can go through Unix was one of the first systems to allow the same code to be
compiled for platforms with different internal representations.
their entire career without One of the first programs converted was supposed to print out
Unix, but on the Series/1 it printed nUxi instead.
masking and shifting
-- https://en.wikipedia.org/wiki/Endianness
• Endianness was just beginning as
a problem in the late 1970’s –
but Intel microprocessors were
little-endian and the rest is
history
Characters, words, and bits in C –
Oh My!
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello world";
int *si = (int *) &s;
int mask, masked, ch; $ a.out
l l e H o W - o 00 d l r
printf(" l l e H o W - o 00 d l r\n"); 6c6c6548 6f77206f 00646c72
printf("%08x %08x %08x\n", si[0], si[1], si[2]); 0000ff00
00006500
mask = 0xff << 8; 00000065 e
masked = si[0] & mask;
ch = masked >> 8;
printf("%08x\n", mask);
Please don’t try this at home
printf("%08x\n", masked); https://en.wikipedia.org/wiki/Endianness
printf("%08x %c\n", ch, ch);
}
kr_02_01.c
Summary
• Number Base Conversion (10, 16, 2)
• Division and a Python 2 story
• Integers
• Words / Bytes
• Characters
Acknowledgements / Contributions
These slides are Copyright 2022- Charles R. Severance (online.dr-chuck.com) Continue new Contributors and Translators here
as part of www.cc4e.com and made available under a Creative Commons
Attribution 4.0 License. Please maintain this last slide in all copies of the
document to comply with the attribution requirements of the license. If you
make a change, feel free to add your name and organization to the list of
contributors on this page as you republish the materials.
Initial Development: Charles Severance, University of Michigan School of
Information
Insert new Contributors and Translators here including names and dates