Recitation 01

The questions below are due on Tuesday September 15, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Unsigned Binary

1)

Write the number 13 as a 4-bit unsigned binary number: (optional)

2)

Write the number 13 as a 6-bit unsigned binary number:

3)

Write the number 6 as unsigned binary number (include only non-zero leading digits): (optional)

4)

What is the decimal equivalent of the binary number 0b1001?

5)

What is the decimal equivalent of the binary number 0b1110? (optional)

6)

What is the range of integers that can be represented with 5 bits? (enter as a list of inclusive numbers: [lower_limit,upper_limit]

7)

How many bits are needed to represent 42?

Summary/Steps to Solutions:

  • For decimal to binary:

    • Repeat division by 2 with remainder being answers in binary (builds LSB to MSB)

    OR...

    • Find the largest power of 2 <= remaining value and subtract until zero. Fill in positions for powers used with 1's and the rest with 0's (builds MSB to LSB)
  • For binary to decimal:

    • For all bits, sum corresponding 2^i (i based on position) and multiply by bit in that position (0 or 1)
  • How to find range # of bits can represent:

    • For unsigned binary representations, 0 to 2^(N)-1 where N is the number of bits

Unsigned Binary Operations

Arithmetic

1)

What is the result of 0b1010+0b1100?:

2)

How many bits are needed to represent result of 0b1010 + 0b1100?

3)

What is the result of 0b1010*0b1111?

4)

How many bits needed to represent result of 0b1010 * 0b1111?

Bitwise

1)

What is the result of ((0b1001 | 0b1100) & 0b0011) << 3?

2)

What is the result of ((~0b0000_1111) ^ 0b1100_1100) >> 4?

Note: The underscores shown in binary numbers don't mean anything special, they just help with readability. Using such syntax in C will result in syntax errors.

Logical

1)

What is the result of (0b100 && (0b000 <= 0b111)) == 0b001?

2)

What is the result of !0b101 || ((0b100-0b011-0b001) && 0b001)?

3)

What is the result of !0b001 == ~0b001?

Hex

1)

What is hexadecimal representation of 0b00011100?

2)

What is the decimal of 0x20?

3)

What is the decimal of 0x16? (optional)

C Types (uint8_t, uint32_t)

1)

To use the minimum number of bits, what type of unsigned variable should we declare to hold the number 17?

2)

To use the minimum number of bits, what type of unsigned variable should we declare to hold the number 500? (optional)

3)

What is the largest number a variable of type uint8_t can store?

4)

What is the largest number a variable of type uint32_t can store?

Note: Other sizes like uint16_t and uint64_t also exist, but the examples are more commonly used.

Division

In C, the division operator (/) behaves differently depending on the operand types:

  • Integer Division (int/int): If both operands are integers, C performs integer division. The resulting decimal part is truncated, not rounded.
  • Floating Point Division (float/int, int/float, float/float): If one or both its operands is a floating point type, C performs floating point division and the decimal part is preserved.

5)

What is the result of 9/2? (optional)

6)

What is the result of 9.0/2? (optional)

7)

What is the value of x?

float x = 9/2 (optional)

Print Statements in C

For each of the following, what will be printed?

1)

printf("%d", 0b111) (optional)

2)

printf("%x", 12) (optional)

3)

printf("%f", 3.0/4.0) (optional)

4)

printf("%c", 65) (optional)

5)

What is the newline character? (optional)

6)

What is the tab character? (optional)

The standard ruleset on string formatting in the printf style is here.

Conditionals and Loops Using Functions

1)

Write out a function that calculates any integer passed in as argument to the power of 10 and returns the result (remember C does not have a primitive exponent operator) (optional)

2)

Write a function which starts with an integer n passed in as argument, updates the number 3n+1 if n is odd, n/2 if n is even, and returns how many times it updated until it hit 1. This is known as the hailstone sequence, where any starting number always returns to 1. (optional)

Variables in Memory

1)

How would you get the memory address of a value stored in a variable x?

2)

If variable z stores the address of the number value 10, how would you get that value 10 from z?

3)

Which of the following are valid ways to declare pointers in C?

4)

Which of the following are valid ways to assign values to a pointer? Assume the line int x = 68; has been run beforehand.

A thinko on pointers:. What will this print out?

#include<stdio.h>
int main(void){
  int v = 16; //(creates v at 0x6B80AF5C in memory)
  int *x = &v;
  x = &v;
  *x = *x**x;
  printf("value at %u is %d\n",(int)x, *x);
}

What will print out when the code above runs?

Scope

1)

Given this piece of code, what will be printed out? Why?

int global[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int main() {
  int global[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  printf("%d", global[0]);
}

2)

Scope is dictated solely by curly braces in C:

#include<stdio.h>

int main() {
  int x = 8;

  {
    int x = 9;
    printf("%d\n", x);
  }

  printf("%d\n", x);
}

2a)

What gets printed first in the code above?

2b)

What gets printed second in the code above?

3)

#include<stdio.h>

int main() {
  int y = 8;

  {
    int x = 9;
    printf("%d\n", x);
  }

  printf("%d\n", x);
}

Will this code compile? (yes or no)? (optional)

Enums + Arrays

1)

Write an enum months_of_year which maps the months of the year, in order, with January starting as 1. (optional)

2)

Write a piece of code that declares an array days_in_month and fill it with months as index that maps to number of days as value. Use the enum you just made and make the 0th value of the array 0. (optional)