Lecture 13 Quiz 2

Joseph Haugh

University of New Mexico

Preface

Assume all code snippets have #include <stdio.h> at the top unless otherwise mentioned.

Question 1

int foo(int n) {
  n = 2*n;
  printf("foo: n=%d ", n);
  return n;
}

void main(void) {
  int n=5;
  printf("main: foo(n)=%d, n=%d\n", 
          foo(n),n);
}

The output of this program is:

  1. foo: n=5 main: foo(n)=5, n=5
  2. foo: n=10 main: foo(n)=10, n=5
  3. foo: n=10 main: foo(n)=10, n=10
  4. foo: n=10 main: foo(n)=20, n=10

Question 1

int foo(int n) {
  n = 2*n;
  printf("foo: n=%d ", n);
  return n;
}

void main(void) {
  int n=5;
  printf("main: foo(n)=%d, n=%d\n", 
          foo(n),n);
}

The output of this program is:

  1. foo: n=10 main: foo(n)=10, n=5

Question 2

void foo(int n[], int i) { 
  n[i] = n[i-2] + n[i-1];
  i = 6;
}

void main(void) { 
  int i, n[11];
  for (i=0; i<11; i++) { 
    n[i] = i*2; 
  }
  i=4;
  foo(n, i);
  printf("%d\n", n[i]);
}

The output of this program is:

  1. 8
  2. 10
  3. 12
  4. 14
  5. 16

Question 2

void foo(int n[], int i) { 
  n[i] = n[i-2] + n[i-1];
  i = 6;
}

void main(void) { 
  int i, n[11];
  for (i=0; i<11; i++) { 
    n[i] = i*2; 
  }
  i=4;
  foo(n, i);
  printf("%d\n", n[i]);
}

The output of this program is:

  1. 10

Question 3

In the C programming language, the ^ operator performs:

  1. Bitwise AND
  2. Bitwise OR
  3. Bitwise XOR
  4. Two’s complement
  5. One’s complement

Question 3

In the C programming language, the ^ operator performs:

  1. Bitwise XOR

Question 4

The binary number, 00101010, in base-ten is:

  1. 101010
  2. 1010
  3. 128
  4. 75
  5. 42

Question 4

The binary number, 00101010, in base-ten is:

  1. 42
128 64 32 16 8 4 2 1
0 0 1 0 1 0 1 0

32 + 8 + 2 = 42

Question 5

The binary number, 10101010, in base-ten is:

  1. 170
  2. 76
  3. 52
  4. 47
  5. 42

Question 5

The binary number, 10101010, in base-ten is:

  1. 170
128 64 32 16 8 4 2 1
1 0 1 0 1 0 1 0

128 + 32 + 8 + 2 = 170

Question 6

The base-ten number, -7, in two’s complement binary is:

  1. 00000111
  2. 11100000
  3. 11111001
  4. 11111000
  5. 11110110

Question 6

The base-ten number, -7, in two’s complement binary is:

  1. 11111001

Two’s complement algorithm:

  1. Write the binary representation of the positive number
  2. Flip all the bits (one’s complement)
  3. Add 1 to the result (ignore any overflow)
00000111
| { one's complement (flip all the bits) }
11111000
| { add 1 }
11111001

Aside: Two’s Complement

The first bit in a two’s complement binary number is the sign bit. If it is 0, the number is positive. If it is 1, the number is negative.

You can add a number and its negative together and get 0.

Operation Decimal Two’s Complement
7 00000111
+ -7 11111001
= 0 00000000

Question 7

void main(void) {
  printf("%d\n", 26 & 28);
}

The output of this program is:

  1. 0
  2. 4
  3. 8
  4. 12
  5. 24

Question 7

void main(void) {
  printf("%d\n", 26 & 28);
}

The output of this program is:

  1. 24
Operation Decimal Binary
26 00011010
& 28 00011100
= 24 00011000

Aside: Decimal to 8 Bit Binary

To convert a decimal number to 8 bit binary:

  1. Test the highest power of 2 which fits into the number of bits, in this case 128.
    • If it is less than the number, write a 1 and subtract the power of 2 from the number.
    • If it is greater than the number, write a 0.
  2. Keep testing repeatedly smaller powers of 2 until your number reaches 0

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
n = 26

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0
n = 26
128 >  26
| { write 0 for 128 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0
n = 26
64  >  26
| { write 0 for 64 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0 0
n = 26
32  >  26
| { write 0 for 32 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0 0 1
n = 26 - 16
16  <= 26
| { write 1 for 16 and subtract 16 from 26 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0 0 1 1
n = 10 - 8
8   <= 10
| { write 1 for 8 and subtract 8 from 10 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0 0 1 1 0
n = 2
4   >  2
| { write 0 for 4 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0 0 1 1 0 1
n = 2 - 2
2   <= 2
| { write 1 for 2 and subtract 2 from 2 }

Aside: Decimal to 8 Bit Binary

For example to convert 26 to 8 bit binary:

128 64 32 16 8 4 2 1
0 0 0 1 1 0 1 0
n = 0
1   >  0
| { write 0 for 1 }

Question 8

void main(void) {
  printf("%d\n", 26 | 28);
}

The output of this program is:

  1. 26
  2. 28
  3. 30
  4. 42
  5. 54

Question 8

void main(void) {
  printf("%d\n", 26 | 28);
}

The output of this program is:

  1. 30
Operation Decimal Binary
26 00011010
| 28 00011100
= 30 00011110

Question 9

void main(void) { 
  unsigned char n = 37;
  unsigned char p = 128;
  int i;
  char bits[9];
  bits[8] = '\0';
  for (i = 0; i <= 7; i++) { 
    if (n & p) bits[i] = '1';
    else bits[i] = '0';
    p = p >> 1;
  }
  printf("%s\n", bits);
}

The output of this program is:

  1. 00110101
  2. 00100101
  3. 00101101
  4. 00100111
  5. 00111101

Question 9

void main(void) { 
  unsigned char n = 37;
  unsigned char p = 128;
  int i;
  char bits[9];
  bits[8] = '\0';
  for (i = 0; i <= 7; i++) { 
    if (n & p) bits[i] = '1';
    else bits[i] = '0';
    p = p >> 1;
  }
  printf("%s\n", bits);
}

The output of this program is:

  1. 00100101

Question 10

void main(void) { 
  unsigned char a = 37;
  unsigned char n;
  int i;

  for (i = 7; i >= 0; i--) {
     n = 1 << i;
     if (!(a & n)) printf("%d, ", n);
  }
  printf("\n");
}

The output of this program is:

  1. 64, 32, 16, 4, 1,
  2. 64, 16, 8, 4, 2, 1,
  3. 64, 16, 8, 4, 1,
  4. 128, 64, 16, 8, 1,
  5. 128, 64, 16, 8, 2,

Question 10

void main(void) { 
  unsigned char a = 37;
  unsigned char n;
  int i;

  for (i = 7; i >= 0; i--) {
     n = 1 << i;
     if (!(a & n)) printf("%d, ", n);
  }
  printf("\n");
}

The output of this program is:

  1. 128, 64, 16, 8, 2,