Computer Systems

[Computer Systems] Chapter.2_Homework problems

ab0utcom 2026. 4. 3. 16:48

2.55

Compile and run the sample code that uses show_bytes (file show-bytes.c) on different machines to which you have access. Determine the byte orderings used by these machines.

 

Sol) show-byte.c는

#include <stdio.h>

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, size_t len) {
    int i;
    for (i = 0; i < len; i++)
    printf("%.2x", start[i]);
    printf("\n");
}

void show_int (int x) {
    show_bytes((byte_pointer) &x, sizeof(int));
}

를 이용하면 된다.

 

간단한

int main()
{
    test_show_bytes(12345);			// 39300000
}

main을 통해 실행해보면 39300000이 나온다.

 

12345는 16진수로 0x0000003039이다.

그리고 출력 결과가 39 30 00 00 (편의를 위해 띄어쓰자)이므로

내 실행 환경은 little endian으로 작동함을 알 수 있다.


2.57

Write procedures show_short, show_long, and show_double that print the byte representations of C objects of types short, long, and double, respectively. Try these out on several machines.

 

Sol)

void show_short(short x) {
    show_bytes((byte_pointer) &x, sizeof(short));
}

void show_long(long x) {
    show_bytes((byte_pointer) &x, sizeof(long));
}

void show_double(double x) {
    show_bytes((byte_pointer) &x, sizeof(double));
}

일단 다음처럼 함수를 작성해볼 수 있고, 이어서 예시 main을 통해 검증해보면

int main()
{
    show_short(127);        // 7f00
    show_long(127);         // 7f00000000000000
    show_double(127.0);     // 0000000000c05f40
}

로 쓸 수 있겠다.

 

short 127은 Bits = $0000 \;0000 \;0111 \;1111_{2}$이므로 $00\;7F_{16}$이고, 이를 little endian으로 표현하니 $7f \; 00$이 나온다.

 

long 127은 Bits = $00\;00\;00\;00\;00\;00\;00\;7F_{16}$이고, little endian으로 표현하면 $7f \;00\;00\;00\;00\;00\;00\;00$이다.

 

double 127.0은 $1.111\;1110\;0000\;... \times 2^{6}$이고, double형의 bias = $2^{11-1}-1 = 1023$이므로 비트 구성은

sign bit = 0

exp bit = $6+1023 = 1029_{10} = 10000000101_{2}$

significand bit = $1111\;1100\;000 ...$이므로

$127.0_{10} = 0100\;0000\;0101\;1111\;1100\;0000\;..._{2}=40\;5F\;C0\;00\;00\;00\;00\;00_{16}$이므로

little edian으로 저장되면 $0x00\;00\;00\;00\;00\;c0\;5f\;40$이다.


2.58

Write a procedure is_little_endian that will return 1 when compiled and run on a little-endian machine, and will return 0 when compiled and run on a bigendian machine. This program should run on any machine, regardless of its word size.

 

Sol)

int is_little() {
    short val = 0x007F;
    unsigned char *p_val = (unsigned char *) &val;
    
    return (p_val[0]==0x7F);     //0x7F00 이면 little, 0x007F이면 big endian
}

2.59

Write a C expression that will yield a word consisting of the least significant byte of x and the remaining bytes of y. For operands x = 0x89ABCDEF and y = 0x76543210, this would give 0x765432EF.

 

Sol)

int main()
{
    unsigned int x = 0x89ABCDEF;
    unsigned int y = 0x76543210;
    
    unsigned int ans = (x&0xFF) | (y&~0xFF);
    printf("%x", ans);				// 765432ef
}

비트마스킹으로 풀 수 있다.


2.60

Suppose we number the bytes in a w-bit word from 0 (least significant) to w/8 − 1 (most significant). Write code for the following C function, which will return an unsigned value in which byte i of argument x has been replaced by byte b: unsigned replace_byte (unsigned x, int i, unsigned char b);

Here are some examples showing how the function should work: replace_byte(0x12345678, 2, 0xAB) --> 0x12AB5678 replace_byte(0x12345678, 0, 0xAB) --> 0x123456AB

 

Sol)

unsigned replace_byte(unsigned x, int i, unsigned char b) {
    unsigned mask = 0xFF << (8*i);
    unsigned ans = (x & ~mask) | (b << (8*i));
    return ans;
}

'Computer Systems' 카테고리의 다른 글

[Computer Systems] Chapter.3_Practice Problem  (0) 2026.04.28