Question:
At present, in self -study Netty, the use of data transmission carrier bytebuf is just learned, and the value obtained by the get method is not understood.
first enter the 1, 2, 3, 4 of Byte type, then enter the INT type 12, and finally enter the BYTE type 5 and 6.
Output buffer.getByte (3) = 4, this is easy to understand, I am aggressive next.
output buffer.getshort (3) = 1024, why?
output buffer.getint (3) = 67108864, why?
Solution:
Generally known:
byte: 1 byte, 8 digits, there are symbols
Short: 2 bytes, 16 bits, symbols
int: 4 bytes, 32 -bit, symbols
Long: 8 bytes, 64 bits, symbols
So:
index getshort(index) index
0—-1: 0000 0001
1 —- 2: 0000 0010 256+2 (0 and 1) 0
2 —- 3: 0000 0011 512+2+1 (1 and 2) 1
3 —– 4: 0000 0100 512+256+4 (2 and 3) 2
4 —- 12: 0000 0000 1024 (3 and 4) 3
5—- 0000 0000
6—- 0000 0000
7—- 0000 1100
8—-5: 0000 0101
9—-6: 0000 0110
Short is 2 bytes, 16 -bit, Getshort (0) reads the combination of 0 and 1 bytes, so it is worth 258;
Getshort (1) Read the combination of 1 and 2 bytes, so the value is 515; (actually not 2 and 3, aggressive)
Getshort (2) Read the combination of 2 and 3 bytes, so the value is 772;
Getshort (3) Read the combination of 3 and 4 bytes, so the value is 1024;
Getint () Get the same value ~
Getshort (0) The combination of 0 and 1 bytes read 0000000100000010 is converted to decimal equivalent to 258
Getshort (1) The combination of 1 and 2 bytes read 0000001000000011 is converted to decimal equal to 515