0% found this document useful (0 votes)
34 views3 pages

Worksheet SLN

This document summarizes pointers and bitwise operators in C. It discusses how pointers store addresses and dereference values, pointer arithmetic, and common bitwise operators like AND, OR, XOR, and NOT. It also covers two's complement representation of signed integers, including how to represent positive and negative numbers, compute sums, and check for overflow. Example code is provided to extract bits, perform subtraction, check equality, determine divisibility, and compare values using only bitwise operators.

Uploaded by

Kiều Long
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Worksheet SLN

This document summarizes pointers and bitwise operators in C. It discusses how pointers store addresses and dereference values, pointer arithmetic, and common bitwise operators like AND, OR, XOR, and NOT. It also covers two's complement representation of signed integers, including how to represent positive and negative numbers, compute sums, and check for overflow. Example code is provided to extract bits, perform subtraction, check equality, determine divisibility, and compare values using only bitwise operators.

Uploaded by

Kiều Long
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE 351 Section 2 – Pointers and Bit Operators

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;
}

Line 1: x Line 2: p x Line 3: p x


410 0x10 410 0x10 350

y y y
350 350 350

Line 4: Line 5: Line 6:


p x p x p x
0x20 350 0x14 350 0x14 351
y y y
350 350 350
C Bitwise Operators
& 0 1 ← AND (&) outputs a 1 only when both input bits are 1. | 0 1
0 0 0 0 0 1
1 0 1 OR (|) outputs a 1 when either input bit is 1. → 1 1 1

^ 0 1 ← XOR (^) outputs a 1 when either input is exclusively 1. ~


0 0 1 0 1
1 1 0 NOT (~) outputs the opposite of its input. → 1 0
Masking is very commonly used with bitwise operations. A mask is a binary constant used to manipulate another
bit string in a specific manner, such as setting specific bits to 1 or 0.

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):

x & 0 = _0____ x | 0 = __x___ x ^ 0 = __x___

x & 1 = _x____ x | 1 = __1___ x ^ 1 = __ 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 subtract(int x, int y) {


return x + ((~y) + 1);
}
Equality: Returns the value of x==y. Allowed operators: >>, &, |, ~, +, ^, !.

int equals(int x, int y) {


return !(x ^ y);
}
Divisible by Eight? Returns the value of (x%8)==0. 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

Exercises: (assume 8-bit integers)


1. What is the largest integer? The largest integer + 1?

Unsigned: Two’s Complement:


1111 1111 -> 0000 0000 0111 1111 -> 1000 0000

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

c. 39 -> 0b 0 0 1 0 0 1 1 1 d. 127 -> 0b 0 1 1 1 1 1 1 1


+(-127)-> 0b 1 0 0 0 0 0 0 1 + 39 -> 0b 0 0 1 0 0 1 1 1
0x A 8 <- 0b 1 0 1 0 1 0 0 0 0x A 6 <- 0b 1 0 1 0 0 1 1 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

You might also like