signed int vs unsigned int

whitehousechef·2024년 3월 4일

signed?

What does signed mean? Remember from our bit and binary class that sign means it has a +/- sign. Unsigned means it is like a raw value

0-positive
1-negative

Max value of signed and unsigned int

Lets say we are comparing 32 bit.

signed

Signed needs to allocate 1 bit to the sign. So the remaining bit we have is 31. So max value of signed 32 bit int is 2^31 -1. There is -1 cuz we want the same range of max positive and min negative number.

For positive, we are including 0 so we have to -1 cuz negative doesnt include 0. Instead for negative, it starts from -1. So the min value of signed int is -2^31. There is no -1 cuz of the range as mentioned.

unsigned

Unsigned is from 0 to 2^32-1. Why minus 1? 0 is either 00000000 or 10000? I didnt know the latter can be represented as 0. So we cant have 1000000000. OHHHHHHH that is cuz the MSB shows the sign and if it is 1 then it is negative. But i thought unsigned doesnt have sign.

So

Instead we need to have 011111111.

ways to represent 0

0 is either 00000000 or 10000? I didnt know the latter can be represented as 0.

look at this https://yaneodoo2.tistory.com/entry/Signed%EC%99%80-Unsigned-1%EC%9D%98-%EB%B3%B4%EC%88%98-2%EC%9D%98-%EB%B3%B4%EC%88%98%EC%9D%98-%EB%AA%A8%EB%93%A0-%EA%B2%83

0개의 댓글