Sem 5‎ > ‎

DCCN LAB

00 All you need to know.

posted Nov 6, 2012, 5:51 PM by Neil Mathew


Awesome PDFs. Summarized Content.

P-3 WAP to generate Hamming Code.

posted Nov 6, 2012, 8:50 AM by Neil Mathew

SOURCE CODE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream> 
//#include<conio.h> 
 
using namespace std;
 
// HAMMING CODE FOR 4-bit data. 2^2 => 2 + 1 = 3
// Error detection bits req = 3
// Total bits required = 4 + 3 = 7 bits
 
// R holding the error detection bits.
// 2^0 = 1
// 2^1 = 2
// 2^2 = 4
 
// Frame: d7 d6 d5 r4 d3 r2 r1
 
int i=0;
int d3, d5, d6, d7;
int r1, r2, r4;
 
 
void sender()
{
        cout<<" Enter the 4-bit word: ";
        cin>>d7;
        cin>>d6;
        cin>>d5;
        cin>>d3;
 
// What all positions it affects:
// r1: 1, 3, 5, 7
// r2: 2, 3, 6, 7
// r4: 4, 5, 6, 7
 
// Then, check whether it contains even number of 1s or not.
// If even, put 0, else put 1.
 
        r1= (d3 + d5 + d7) %2;
        r2= (d3 + d6 + d7) %2;
        r4= (d5 + d6 + d7) %2;
 
// FRAME:
        int frame[] = { r1, r2, d3, r4, d5, d6, d7 };
 
        cout <<"\n The code at SENDER is: ";
        for(i=6; i>=0; i--)
        cout<<frame[i]<<" ";
}
 
void receiver()
{
        int frame[7];
        cout<<"\n Enter the code at RECEIVER: ";
 
        for(i=6; i>=0; i--)
        cin>>frame[i];
 
        d7 = frame[6];
        d6 = frame[5];
        d5 = frame[4];
        r4 = frame[3];
        d3 = frame[2];
        r2 = frame[1];
        r1 = frame[0];
 
// What all positions Rs it affects:
// r1: 1, 3, 5, 7
// r2: 2, 3, 6, 7
// r4: 4, 5, 6, 7
 
        //If not even
        r1= (r1 + d3 + d5 + d7) %2;
        r2= (r2 + d3 + d6 + d7) %2;
        r4= (r4 + d5 + d6 + d7) %2;
 
//Find position by converting binary no r4 r2 r1 to decimal
 
        int dec=0;
 
        if( r1 == 0 && r2 == 0 && r4 == 0 )
        cout<<" No Errors. Message is = "<<d7<<d6<<d5<<d3;
        else
        {
        if( r4 == 1)
        dec += 4;
 
        if(r2 == 1)
        dec += 2;
 
        if(r1 == 1)
        dec += 1;
 
        cout<<"\n Error found at position "<<dec;
        }
}
 
int main ( )
{
//      clrscr();
 
        cout<<"\n FRAME: d7 d6 d5 r4 d3 r2 r1 "<<endl;
        cout<<endl;
        sender();
 
        cout<<endl;
        receiver();
        cout<<endl;
//      getch();
}


OUTPUT:

 FRAME: d7 d6 d5 r4 d3 r2 r1 

 Enter the 4-bit word: 1 1 0 0
 The code at SENDER is: 1 1 0 0 0 0 1 

 Enter the code at RECEIVER: 1 1 0 0 0 0 1 
 No Errors. Message is = 1100
 FRAME: d7 d6 d5 r4 d3 r2 r1 

 Enter the 4-bit word: 1 1 0 0
 The code at SENDER is: 1 1 0 0 0 0 1 

 Enter the code at RECEIVER: 1 1 0 0 1 0 1
 Error found at position 3

P-2 WAP to determine if the IP address is in Class A, B, C, D, or E.

posted Nov 6, 2012, 5:58 AM by Neil Mathew   [ updated Nov 6, 2012, 6:00 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
 
using namespace std;
 
/*      CLASSES
 
        A:        0 - 127 ( = 128 )
        B:      128 - 191 ( =  64 )
        C:      192 - 223 ( =  32 )
        D:      224 - 239 ( =  16 )
        E:      240 - 255 ( =  16 )
*/
 
 
int main()
        {
//      clrscr();
 
        int a[4],i=0;
 
        cout<<"Enter The IP address";
        for(i=0;i<4;i++)
                cin>>a[i];
 
        cout<<"\n IP ADDRESS:"<<a[0];
        cout<<"."<<a[1]<<"."<<a[2]<<"."<<a[3]<<"\n";
 
        cout<<"The IP address is in Class: ";
 
        if(a[0]>=0 && a[0]<=127)
        cout<<"Class A";
        if(a[0]>127 && a[0]<191)
        cout<<"Class B";
        if(a[0]>191 && a[0]<224)
        cout<<"Class C";
        if(a[0]>224 && a[0]<=239)
        cout<<"Class D";
        if(a[0]>239)
        cout<<"Class E";
 
//      getch();
 
        return 1;
        }
 


OUTPUT:


Enter The IP address 220 100 8 12

 IP ADDRESS:220.100.8.12
The IP address is in Class: Class C

02 IP Addresses? Their Classes.

posted Nov 6, 2012, 5:47 AM by Neil Mathew   [ updated Nov 6, 2012, 6:12 AM ]


BRIEF INTRO:

IP: Short for Internet ProtocolIP is an address of a computer or other network device on a network using IP or TCP/IP.

There are five classes of available IP ranges: Class A, Class B, Class C, Class D and Class E, while only A, B, and C are commonly used. Each class allows for a range of valid IP addresses. Below is a listing of these addresses.


ClassAddress RangeSupports
Class A1.0.0.1 to 126.255.255.254Supports 16 million hosts on each of 127 networks.
Class B128.1.0.1 to 191.255.255.254Supports 65,000 hosts on each of 16,000 networks.
Class C192.0.1.1 to 223.255.254.254Supports 254 hosts on each of 2 million networks.
Class D224.0.0.0 to 239.255.255.255Reserved for multicast groups.
Class E240.0.0.0 to 254.255.255.254Reserved for future use, or Research and Development Purposes.


Every IP address is broke down into four sets of octets that break down into binary to represent the actual IP address. The below table is an example of the IP 255.255.255.255. 


IP:255255255255
Binary value:11111111111111111111111111111111
Octet value:8888


If we were to break down the IP "166.70.10.23", you would get the below value. In the below table, the first row is the IP address, the second row is the binary values, and the third row shows how the binary value equals the section of the IP address.

166701023
10100110010001100000101000010111
128+32+4+2=16664+4+2=708+2=1016+4+2+1=23



P-1 WAP to translate dotted decimal IP address into 32 bit address.

posted Nov 6, 2012, 5:36 AM by Neil Mathew


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
 
using namespace std;
 
void DEC2BIN(int dec)
{
        // 128 = 2^7 = 8 bit
        //  64 = 2^6 = 7 bit
        //  32 = 2^5 = 6 bit
        //  16 = 2^4 = 5 bit
        //  08 = 2^3 = 4 bit
        //  04 = 2^2 = 3 bit
        //  02 = 2^1 = 2 bit
        //  01 = 2^0 = 1 bit  
 
  for (int i = 128; i != 0; i=i>>1)
  {
    if (dec & i)
      cout<<"1";
    else
      cout<<"0";
  }
 
}
 
 
int main()
        {
 
        //clrscr();
 
        //DECLARATIONS
        int i,j;
        int dec[4];
        int bin[8]={128,64,32,16,8,4,2,1};
 
        //INPUT
        cout<<"Enter the IP Address: ";
        for(i=0;i<4;i++)
        cin>>dec[i];
 
        //TRANSLATION
        cout<<"The ip address is: "<<dec[0];
        cout<<"."<<dec[1]<<"."<<dec[2]<<"."<<dec[3]<<endl;
 
        for(i=0; i<4; i++)
                {
                DEC2BIN(dec[i]);        
 
                if(i!=3)
                cout<<".";
                }
 
        //getch();
        return 1;
        }
 



OUTPUT:

Enter the IP Address: 100 125 0 8
The ip address is: 100.125.0.8
01100100.01111101.00000000.00001000

P-0b WAP to convert Binary to Decimal

posted Nov 6, 2012, 12:40 AM by Neil Mathew


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
 
using namespace std;
 
int pow(int n, int i)
{
int result=1;
 
while(i--)
{
result*=n;
}
 
return result;
}
 
 
int main()
{
  int n;
 
  cout<<"Enter A Binary Number: ";
  cin>>n;
 
  cout<<"\n BINARY: "<<n;
 
        // 128 = 2^7 = 8 bit
        //  64 = 2^6 = 7 bit
        //  32 = 2^5 = 6 bit
        //  16 = 2^4 = 5 bit
        //  08 = 2^3 = 4 bit
        //  04 = 2^2 = 3 bit
        //  02 = 2^1 = 2 bit
        //  01 = 2^0 = 1 bit
 
        int i=0; //1st place
        int dec=0;
        int digit;
        
        do
        {
        digit = n%10;
        
        if( digit == 1 )
        dec+=pow(2,i);
 
        i++;
        n/=10;
        }
        while (n > 0);
        
 
  cout<<"\n DECIMAL: "<<dec<<endl;
        return 1;
}

OUTPUT:


Enter A Binary Number: 
 BINARY: 1100
 DECIMAL: 12

P-0 WAP to Convert Decimal to Binary Number

posted Nov 6, 2012, 12:18 AM by Neil Mathew



Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream.h>
#include <conio.h>
void main()
{
     clrscr();
  int n;
 
  cout<<"Enter A Decimal Number: ";
  cin>>n;
 
  cout<<"\n DECIMAL: "<<n;
 
        // 128 = 2^7 = 8 bit
        //  64 = 2^6 = 7 bit
        //  32 = 2^5 = 6 bit
        //  16 = 2^4 = 5 bit
        //  08 = 2^3 = 4 bit
        //  04 = 2^2 = 3 bit
        //  02 = 2^1 = 2 bit
        //  01 = 2^0 = 1 bit
 
  cout<<"\n BINARY: ";
 
  for (int i = 128; i != 0; i=i>>1)
  {
    if (n & i)
      cout<<"1 ";
    else
      cout<<"0 ";
  }
 
  cout<<endl;
  getch();
 


Output:

Enter A Decimal Number: 12
 DECIMAL: 12
 BINARY: 0 0 0 0 1 1 0 0 

01 Understanding Bit-wise Operators

posted Nov 5, 2012, 11:49 PM by Neil Mathew   [ updated Nov 6, 2012, 6:11 AM ]


First of all, we need to understand how the bitwise operator works.



Bitwise operators are kind of advanced - they work on the binary bits of numbers. 

First, you have bitwise AND(&).
 This compares each bit. 
If the bits from both of the numbers are 1, then the resulting bit is 1. 
Otherwise, the resulting bit is 0. 

For example:

 56 00111000 
235 11101011
&------------- 
 40 00101000


Next, you have bitwise OR(|)
If either bit is 1, then the resulting bit is 1.

 56 00111000
235 11101011 
|-------------
251 11111011 


Also, you have bitwise XOR(^)
If only one of the bits is 1, then the resulting bit is 1 
(if both are 1, or neither are 1, then the resulting bit is 0).

 56 00111000
235 11101011
^---------------
211 11010011


The final bitwise operator is bitwise NOT (~). 
This works on only one operand - it simply flips the bits.

 56 00111000
~-------------
199 11000111



Second, we need to know HOW they can be used.

One main function is the easy conversion of decimal values to binary and vice versa. 

We can use two operations to perform a number conversion.
1. Bit-wise AND operation. To compare the bits one by one.
2. Bit-wise Right - Shift Operation. To divide the number by 2.

Depending on the no of bits in the system, we need an initial constant.

For 4-bit numbers, we use 8 (1000)

Each step, we divide this constant by 2 using bit-wise right shift operator >>.
8>>1 = 4
4>>1 = 2
2>>1 = 1

( Operandtoshift >> NoOfTimesDone )

step-1
======

1 1 0 0 = 12
1 0 0 0 = 08
&-----------

1 0 0 0 (12 & 8)

Not Zero. Print 1.




1
 step-2
======
1 1 0 0 = 12
0 1 0 0 = 04
&-----------

0 1 0 0 
(12 & 4)

Not Zero. Print 1.




1
 
 step-3
======
1 1 0 0 = 12
0 0 1 0 = 02
&-----------

0 0 0 0 
(12 & 2)

Zero. Print 0.




0 
step-4
======
1 1 0 0 = 12
0 0 0 1 = 01 
&-----------

0 0 0 0 (12 & 1)

Zero. Print 0.

 


0


So, Binary equivalent of 12 = 1100

1-8 of 8