Worksheet SLN
Worksheet SLN
Pointers
A pointer is a variable that holds an address. C uses pointers explicitly. If we have a variable x, then &x gives the
address of x rather than the value of x. If we have a pointer p, then *p gives us the value that p points to, rather
than the value of p.
Consider the following declarations and assignments:
int x;
int *ptr; 1)
ptr = &x;
1) We can represent the result of these three lines of code visually as shown.
The variable ptr stores the address of x, and we say “ptr points to x.” 2)
x currently doesn’t contain a value since we did not assign x a value!
2) After executing x = 5;, the memory diagram changes as shown.
3) After executing *ptr = 200;, the memory diagram changes as shown. 3)
We modified the value of x by dereferencing ptr.
Pointer Arithmetic
In C, arithmetic on pointers (++, +, --, -) is scaled by the size of the data type the pointer points to. That is, if p is
declared with pointer type* p, then p + i will change the value of p (an address) by i*sizeof(type) (in
bytes). However, *p returns the data pointed at by p, so pointer arithmetic only applies if p was a pointer to a
pointer.
Exercise:
Draw out the memory diagram after sequential execution of each of the lines below:
int main(int argc, char **argv) {
int x = 410, y = 350; // assume &x = 0x10, &y = 0x14
int *p = &x; // p is a pointer to an integer
*p = y;
p = p + 4;
p = &y;
x = *p + 1;
}
y y y
350 350 350
Exercises:
1) What happens when we fix/set one of the inputs to the 2-input gates? Let x be the other input.
Fill in the following blanks with either 0, 1, x, or x̅ (NOT x):
2) Lab 1 Helper Exercises: Lab 1 is intended to familiarize you with bitwise operations in C through a series of
puzzles. These exercises are either sub-problems directly from the lab or expose concepts needed to complete
the lab. Start early!
Bit Extraction: Returns the value (0 or 1) of the 19th bit (counting from LSB). Allowed operators: >>, &, |, ~.
int extract19(int x) {
return (x >> 18) & 0x1;
}
Subtraction: Returns the value of x–y. Allowed operators: >>, &, |, ~, +.
int divisible_by_8(int x) {
return !((x << 29);
}
Greater than Zero? Returns the value of x>0. Allowed operators: >>, &, |, ~, +, ^, !.
int greater_than_0(int x) {
/* invert and check sign; we need the third operand for the T_min case */
return ((~x + 1) >> 31) & 0x1 & ~(x >> 31)_OR_!!x & ~(x >> 31);
}
Signed Integers with Two’s Complement
Two’s complement is the standard for representing signed integers:
• The most significant bit (MSB) has a negative value; all others have positive values (same as unsigned)
• Binary addition is performed the same way for signed and unsigned
• The bit representation for the negative (additive inverse) of a two’s complement number can be found by:
flipping all the bits and adding 1 (i.e. −𝒙 = ~𝒙 + 𝟏).
The “number wheel” showing the relationship between 4-bit numerals and
their Two’s Complement interpretations is shown on the right:
• The largest number is 7 whereas the smallest number is -8
• There is a nice symmetry between numbers and their negative
counterparts except for -8
2. How do you represent (if possible) the following numbers: 39, -39, 127?
Unsigned: Two’s Complement:
39: 0010 0111 39: 0010 0111
-39: Impossible -39: 1101 1001
127: 0111 1111 127: 0111 1111
3. Compute the following sums in binary using your Two’s Complement answers from above. Answer in hex.
a. 39 -> 0b 0 0 1 0 0 1 1 1 b. 127 -> 0b 0 1 1 1 1 1 1 1
+(-39) -> 0b 1 1 0 1 1 0 0 1 + (-39)-> 0b 1 1 0 1 1 0 0 1
0x 0 0 <- 0b 0 0 0 0 0 0 0 0 0x 5 8 <- 0b 0 1 0 1 1 0 0 0
4. Interpret each of your answers above and indicate whether or not overflow has occurred.
a. 39 + (-39) b. 127 + (-39)
Unsigned: 0 overflow Unsigned: 88 overflow
Two’s Complement: 0 no overflow Two’s Complement: 88 no overflow
c. 39 + (-127) d. 127 + 39
Unsigned: 168 no overflow Unsigned: 166 no overflow
Two’s Complement: -88 no overflow Two’s Complement: -90 overflow