JYVSKYLN YLIOPISTO Bittioperaatiot Javalla TIEA 322 Tietoliikenneprotokollat Ari
JYVÄSKYLÄN YLIOPISTO Bittioperaatiot Javalla TIEA 322 Tietoliikenneprotokollat Ari Viinikainen
JYVÄSKYLÄN YLIOPISTO Kehysrakenne D M Y Syntymäaika D = päivä (5 bittiä) M = kuukausi (4 bittiä) Y = vuosi (7 bittiä) Yhteensä 16 bittiä, 2 tavua Kuinka asetetaan? 2
JYVÄSKYLÄN YLIOPISTO Päivämäärän asetus 16 bittiin D M Y int D=31; int M=12; int Y=99; // byte, short DMY=0; // 16 bittiä, 2 tavua DMY = (short)(DMY ^ (D << 11)); DMY = (short)(DMY ^ (M << 7)); DMY = (short)(DMY ^ Y); 3
JYVÄSKYLÄN YLIOPISTO Päivän asetus kenttään D M Y int D=31; int M=12; int Y=99; DMY = (short)(DMY ^ (D << 11)); D = 3110 = D << 1110 = XOR 000000111112 111110000002 00000000 1111100000000000 DMY D << 11 uusi DMY 4
JYVÄSKYLÄN YLIOPISTO Kuukauden asetus kenttään D M Y int D=31; int M=12; int Y=99; DMY = (short)(DMY ^ (M << 7)); M = 1210 = M << 710 = XOR 00000011002 0000011000002 111110000001100000 111111100000 DMY M << 7 uusi DMY 5
JYVÄSKYLÄN YLIOPISTO Vuoden asetus kenttään D M Y int D=31; int M=12; int Y=99; DMY = (short)(DMY ^ Y); Y = 9910 = XOR 0000011000112 1111111000001100011 111111100011 DMY Y uusi DMY 6
JYVÄSKYLÄN YLIOPISTO Sokettiin kirjoittaminen int D=31; int M=12; int Y=99; DMY on short tyyppinen, sokettiin pitää kirjoittaa tavuja byte[] data = new byte[2]; data[0] = (byte)(DMY >>> 8); 111111100011 >>> 8 = 000011111110 (byte) 000011111110 = 11111110 data[1] = (byte)(DMY & 0 xff); AND 111111100011 00001111 000001100011 // Lähetetään data soketin kautta maailmalle 7
JYVÄSKYLÄN YLIOPISTO Vastaanotto ja yhdistäminen D M Y byte[] receive. Data = new byte[2]; Short Rec_dmy; // Saadaan soketista 2 tavua receive. Data taulukkoon Rec_dmy = (short)(receive. Data[0] & 0 xff); Rec_dmy = Rec_dmy << 8; Rec_dmy = (short)(Rec_dmy ^ (receive. Data[1] & 0 xff)); 8
JYVÄSKYLÄN YLIOPISTO Vastaanotto binäärisenä //receive. Data[0] = 11111110; receive. Data[1] = 01100011 Rec_dmy = (short)(receive. Data[0] & 0 xff); AND 11111110 receive. Data[0] 1111 0 x. FF (maski) 11111110 (tulos Javassa int) 000011111110 (typecast to short) Rec_dmy = Rec_dmy << 8; 000011111110 0000 Rec_dmy << 8 Rec_dmy = (short)(Rec_dmy ^ (receive. Data[1] & 0 xff)); XOR 11111110 0000 Rec_dmy 01100011 receive. Data[1] & 0 xff 11111110 01100011 XOR: n int tyyppimuunnetu short: iksi 9
JYVÄSKYLÄN YLIOPISTO Vastaanotto ja dekoodaus D M Y byte[] rec_data = new byte[2]; Short Rec_dmy; Rec_dmy = (short)(receive. Data[0] & 0 xff); Rec_dmy = Rec_dmy << 8; Rec_dmy = (short)(Rec_dmy ^ (receive. Data[1] & 0 xff)); byte day = (byte) ((Rec_dmy >>> 11) & 0 x 1 F); byte month = (byte) ((Rec_dmy >>> 7) & 0 x. F); byte year = (byte) (Rec_dmy & 0 x 7 F); 10
JYVÄSKYLÄN YLIOPISTO Kenttien arvot purettuna //Rec_dmy = 111111100011 byte day = (byte) ((Rec_dmy >>> 11) & 0 x 1 F); AND Rec_dmy 0 x 1 F == 31 0000000000011111 >>> 11 (shifting) (maski) (tulos) byte month = (byte) ((Rec_dmy >>> 7) & 0 x. F); AND Rec_dmy 0 x. F == 12 0000000111111100 0000001111 0000001100 >>> 7 byte year = (byte) (Rec_dmy & 0 x 7 F); AND 111111100011 000001111111 000001100011 Rec_dmy 0 x 7 F == 99 11
- Slides: 11