one line of code at a time

Review COMP 211: Systems Fundamentals 본문

컴퓨터공부

Review COMP 211: Systems Fundamentals

oloc 2024. 8. 25. 05:35

The intuition of Two's Complement Representation of Signed Integers

- Big idea: Negate the value of the highest order bit and treat every other bit the same.

- 가장 큰 bit에 +/-의 의미를 부여하고, 나머지 bit는 똑같음.

 

Negation of a Two's Complement Value

- To negate any signed-value with Two's Complement representation: 

(1) Take the complement of all bits (0 to 1, 1 to 0)

(2) Add 1


The least significant bit (LSB) of a binary number is the rightmost bit, which determines whether the number is odd or even.

What is the least significant bit of the number 0b0100_1101_0101_1001? Prepend your answer with 0b.

0b1
 
The most significant bit (MSB) is the bit in a binary number that represents the highest value place. It is the leftmost bit in the number. In terms of significance, it determines the number's magnitude the most.
 

0b 00_1111의 unsigned, sign-magnitude, 2's complement 구하기.

 

2's complement는 컴퓨터에서 음수를 표현하는 일반적인 방식이다.

 

(1) unsigned를 구할 때는 부호를 생각하지 말고, 모든 비트를 그냥 숫자로 생각하면 된다. 따라서 여기서는 1+2+4+8=15

(2) sign-magnitude의 경우, 가장 왼쪽에 있는 비트는 부호를 나타낸다. 0은 positive (양수), 1은 negative (음수)를 나타낸다. magnitude는 0b0_1111 이므로 +15, 즉 15이다.

(3) 양수일 때는 비트가 변경되지 않고 그대로 유지되므로 2's complement와 unsigned, sign-magnitude 값이 같다.

 

0b 10_0001의 unsigned, sign-magnitude, 2's complement 구하기.

(1) unsigned는 1+32 = 33

(2) sign-magnitude는 왼쪽 비트를 부호로 사용하고, 나머지 비트를 숫자 크기로 사용하면 된다. 맨 왼쪽 비트가 1이니까 음수이고, 0_0001은 1이니까 값은 -1이다.

(3) 맨 왼쪽 비트가 0일 때는 양수이므로 아무것도 할 필요가 없다. 그런데 1일 때는 음수이므로 음수값을 구해야 한다. 음수값을 구하는 방법은 첫 번째로 부호 비트를 제외한 나머지 비트를 뒤집는다 (1은 0으로, 0은 1로). 그런 다음 1을 더해준다. 1_1110인데 1을 더하면 1_1111이다. 1+2+4+8+16 = 31인데 음수이므로 -31이다.

 

22를 4-bit로 표현하고자 하면?

이럴 경우 오버플로우가 발생한다. 표현을 하면, 모듈로 연산을 통해서 4-bit 범위 내의 값을 계산하게 된다. 22 % 16 = 6은 0b0110.

 

이진수 더하기

0b1_1010과 0b1_0101 을 더하면 5 bit로 표현하고자 할 때 overflow가 발생한다. 그러면 6th bit를 버린다.

 

2's Complement Binary Addition

- 2개의 같은 부호의 숫자가 다른 부호를 만들어내면 overflow가 발생한 것이다. 다른 부호의 숫자를 더하면 overflow가 발생하지 않는다.

 

Bitwise Operation

0b0011 << 2 is a bitwise left shift operation. Shifting left by 2 positions moves each bit two places to the left and fills the rightmost bits with zeros.

 

Logical Shift vs. Arithmetic Shift

https://open4tech.com/logical-vs-arithmetic-shift/

 

Memory

Address of element n = Base Address + (n * size of each element)

 

I have a 20-element integer array whose base address is 0x00030010. sizeof(int) = 4 bytes

 

What is the address of element 2?

0x00030010 + 2 * 4 (0x00000008) = 0x00030018

 

What is the address of element 9?

0x00030010 + 9 * 4 (0x00000024) = 0x00030034

 

Sign Extension

- To extermine a number from a smaller bit width to a larger one while preserving its value, we need to use sign extension.

- 0b111 이라는 3-bit 숫자를 8-bit로 바꾸려고 한다. 가장 왼쪽 비트가 1이므로 2's complement noation 상에서 이 숫자는 음수다. 

- Sign Extension 할 때는 양수면 (MSB = 0) extend with 0s and 음수면 (MSB = 1) extend with 1s. 

 

Zero Extension

- 왼쪽 비트를 0으로 채우는 것

 

Store Integer Array

- typically integers are stored in little-endian format in memory, meaning the least significant byte is stored at the smallest address.

- Each integer is 4 bytes (32 bits) in size.

 

The following integer array is stored in memory starting at address 0x7FFF0000.
arr =
[42185 100492 252475]

https://www.rapidtables.com/convert/number/decimal-to-hex.html

42185를 hex로 바꾸면, 0xA4C9이고, byte 단위로 쪼개면 '0xA4', '0xC9'가 되는데 전자가 MSB, 후자가 LSB이므로 LSB인  

0xC9부터 메모리에 저장된다. 0xC9를 binary로 바꾸면 0b1100_1001이므로 A에는 0b1100_1001가 저장된다.

 


참고

 

'컴퓨터공부' 카테고리의 다른 글

MIPS Read and Write in Register file  (1) 2024.10.12
Bits, Nibbles, and Bytes in Memory  (0) 2024.08.24
nMos, pMos 트랜지스터  (0) 2024.08.24