문제

struct bitCard {
   unsigned int face : 4;
   unsigned int suit : 2;
   unsigned int color : 1;
};

"The preceding structure definition indicates that member face is stored in 4 bits, member suit is stored in 2 bits and member color is stored in 1 bit. The number of bits is based on the desired range of values for each structure member. Member face stores values from 0(Ace) through 12(King)—4 bits can store values in the range 0–15." (C how to program). The sentence in bold makes me confused since I cannot help myself understand why 4 bits can store values from 0 to 15. Can anyone help me out?

도움이 되었습니까?

해결책

think 2 in the power of 4 = 16 possible values... binary value represent decimal number in range 0..2^x - where X=num of bits

다른 팁

Because 1+2+4+8 = 15 it follows that 4-digit binary numbers can range from 0 to 15. The numbers are:

0000b =  0d
0001b =  1d
0010b =  2d
0011b =  3d
...
1110b = 14d
1111b = 15d

as there only 16 unique combinations (thanks Udo) of 0,1 that you can represent with 4 bits

 0000  0
 0001  1 
 0010  2 
 0011  3
.........
 1111  15

because 15 = 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1*2^0 ie: 15 = 0xF = 1111b

Where ^ is the power operation.

Or, more detailed:

0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = 10
1011 = 11
1100 = 12
1101 = 13
1110 = 14
1111 = 15

I really can recommend reading the wikipedia article about binary numbers: http://en.wikipedia.org/wiki/Binary_number

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top