Joseph Haugh
University of New Mexico
Assume all code snippets have #include <stdio.h>
at the top unless otherwise mentioned.
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:
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:
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:
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:
In the C programming language, the ^ operator performs:
In the C programming language, the ^ operator performs:
The binary number, 00101010, in base-ten is:
The binary number, 00101010, in base-ten is:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
32 + 8 + 2 = 42
The binary number, 10101010, in base-ten is:
The binary number, 10101010, in base-ten is:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
128 + 32 + 8 + 2 = 170
The base-ten number, -7, in two’s complement binary is:
The base-ten number, -7, in two’s complement binary is:
Two’s complement algorithm:
00000111
| { one's complement (flip all the bits) }
11111000
| { add 1 }
11111001
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 |
void main(void) {
printf("%d\n", 26 & 28);
}
The output of this program is:
void main(void) {
printf("%d\n", 26 & 28);
}
The output of this program is:
Operation | Decimal | Binary |
---|---|---|
26 | 00011010 | |
& | 28 | 00011100 |
= | 24 | 00011000 |
To convert a decimal number to 8 bit binary:
For example to convert 26 to 8 bit binary:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
n = 26
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
void main(void) {
printf("%d\n", 26 | 28);
}
The output of this program is:
void main(void) {
printf("%d\n", 26 | 28);
}
The output of this program is:
Operation | Decimal | Binary |
---|---|---|
26 | 00011010 | |
| | 28 | 00011100 |
= | 30 | 00011110 |
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:
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:
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:
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: