Recitation 01
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)
2)
3)
4)
5)
6)
[lower_limit,upper_limit]
7)
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)
2)
3)
4)
Bitwise
1)
2)
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)
2)
3)
Hex
1)
2)
3)
C Types (uint8_t, uint32_t)
1)
2)
3)
uint8_t can store?
4)
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)
9/2? (optional)
6)
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)
6)
The standard ruleset on string formatting in the printf style is here.
Conditionals and Loops Using Functions
1)
2)
Variables in Memory
1)
x?
2)
z stores the address of the number value 10, how would you get that value 10 from z?
3)
4)
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);
}
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)
2b)
3)
#include<stdio.h>
int main() {
int y = 8;
{
int x = 9;
printf("%d\n", x);
}
printf("%d\n", x);
}
Enums + Arrays
1)
months_of_year which maps the months of the year, in order,
with January starting as 1. (optional)
2)
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)