List of Java Programs for Binary Conversion (CCN-Computer Communication Networking GLS Msc-it 2016)


1. Java program to implement bitwise operator.

2. Java program to convert a decimal to binary using bitwise operator.

3. Java program for Decimal to binary masking (with array and String).

4. Program to convert decimal to binary and insert bit value in it to get back the new decimal value-Static way.

5. Java program to extract bit value.

6. java program to count char for string output as framing-data link layer.

7. Java program to implement bit stuffing.

8. Java program to implement byte stuffing.

9. Java program to represent how parity bit works.



//1.
Java program to implement bitwise operator.

class DjOp
{
public static void main(String args[])
{
int a=60;
int b=13;

System.out.println("\n Bitwise AND: "+(a & b));

System.out.println("\n Bitwise OR: "+(a | b));

System.out.println("\n Bitwise XOR: "+(a ^ b));

System.out.println("\n Bitwise Compliment: "+(~a));

System.out.println("\n Left shift: "+(a << 2));

System.out.println("\n Right shift: "+(a >> 2));

System.out.println("\n Zero fill right shift: "+(a >>>2));
}
}



//2. Java program to convert a decimal to binary using bitwise operator.

class DjdbBit
{
public static void main(String args[])
{
int v=60, i=0;
int m=1;
int ans;
m=m<<7;
//System.out.print(m);
while(i!=8)
{
ans=v&m;
m=m>>1;
//System.out.println("yes    "+ans);
if(ans==0)
System.out.print("0");

else
System.out.print("1");

i++;
}
//System.out.println("\n");
}
}



//3.1. Java program for Decimal to binary masking with array.


import java.util.*;
class DjMask
{
public static void main(String args[])
{
int[] ans=new int[6];
int i,j,k,l,no;
int on=1,off=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter value for masking: ");
no=s.nextInt();

for(i=0;i<6;i++)
{
int temp=no%2;
ans[i]=temp;
no=no/2;
}
System.out.println("Decimal to binary: ");
for(j=5;j>=0;j--)
{
System.out.print(ans[j]);
}
System.out.println("\nOn to Off masking: ");
for(k=5;k>=0;k--)
{
System.out.print(ans[k] & off);
}
System.out.println("\nOff to On masking: ");
for(l=5;l>=0;l--)
{
System.out.print(ans[l] | on);
}
}
}

//3.2. Java program for Decimal to binary masking with string.

import java.util.*;
class DjMask2
{
public static void main(String args[])
{
String ans="";
String on="";
String off="";
int temp,no;
Scanner s=new Scanner(System.in);
System.out.println("Enter value for masking: ");
no=s.nextInt();

while(no>0)
{
temp=no%2;
ans=ans+temp;
no=no/2;
on=on+(temp | 1);
off=off+(temp & 0);
}

StringBuffer b1=new StringBuffer(ans);
System.out.println("Decimal to binary: "+b1.reverse());

System.out.println("Off to On masking: "+on);
System.out.println("On to Off masking: "+off);
}
}



//4. Program to convert decimal to binary and insert bit value in it to get back the new decimal value-
Static way.

import java.util.*;
class djInsBit
{
public static void main(String args[])
{
    int no=10;
        int temp,left,right,ans,l;
        
        right=no>>2;
        temp=10>>2;
        int t=temp<<1;
        left=t|1;
        l=left<<2;
        ans=l|right;
        
        System.out.println("Ans of binary insert: "+ans);
    }

}



//5. Java program to extract bit value.

import java.util.*;
class DjExtract
{
public static void main(String args[])
    {
    int no,pos,mask=1;
        System.out.println("Enter no: ");
        Scanner s=new Scanner(System.in);
        no=s.nextInt();
        
        System.out.println("Enter position: ");
        pos=s.nextInt();
        int temp=mask<<pos-1;
        int ans=temp&no;
        
        System.out.print("position is "+pos+" ans is "+ans);
    }
}



//6. java program to count char for string output as framing-data link layer.

import java.util.*;
class Frame1
{
int ch;
    String nm[ ]=new String[5];
    String name;
    int i=0,j;
    public void value()
    {
    Scanner s=new Scanner(System.in);
        Scanner s1=new Scanner(System.in);
        do
        {
        System.out.println("Enter your value for framming: ");
            nm[i]=s.nextLine();
            i++;
            System.out.println("press 1 to continue..");
            ch=s1.nextInt();
        }while(ch==1);
    }
    public void ans()
    {
for(j=0;j<i;j++)
        {
        System.out.println((nm[j].length()+1)+" , "+nm[j]);
        }    
    }
}
public class djFrame
{
public static void main(String args[])
    {
    Frame1 obj=new Frame1();
            obj.value();
            obj.ans();
    }
}



//7. Java program to implement bit stuffing.

import java.util.*;
class DjBitStuffing
{
public static void main(String args[])
{
int no[]=new int[15];
int count=0;
int i,j;
System.out.print("Enter your binary value: ");
Scanner s=new Scanner(System.in);

for(i=0;i<15;i++)
{
no[i]=s.nextInt();
}
System.out.println("Bit Stuffing value: ");
for(j=0;j<15;j++)
{
count++;
if(no[j]==0)
{
count=0;
}
if(count==5)
{
no[j+1]=0;
}
System.out.print(no[j]);
}
}
}



//8. Java program to implement byte stuffing. 

import java.util.*;
class DjByteStuffing
{
public static void main(String args[])
{
String msg="";
String start="DLE STX";
String end="DLE ETX";
String add="DLE";
String result=start;
int i;

System.out.println("Enter your value for byte stuffing: ");
Scanner s=new Scanner(System.in);
msg=s.nextLine();

for(i=0;i<msg.length()-2;i++)
{
if(msg.charAt(i)=='d' &&
msg.charAt(i+1)=='l' &&
msg.charAt(i+2)=='e')
{
msg=msg+add;
}
}
result=result+msg;
result=result+end;
System.out.println(result);
}

}



//9. Java program to represent how parity bit works. 

import java.util.*;
class DjParity1
{
String msg=" ";
int i,count=0;
Scanner s=new Scanner(System.in);
public void evenParity()
{
System.out.println("Enter your bit value: ");
msg=s.nextLine();
for(i=0;i<msg.length();i++)
{
if(msg.charAt(i)=='1')
{
count++;
}
}
if(count%2==1)
msg='1'+msg;
else
msg='0'+msg;

System.out.println("ans: "+msg);
}
public void oddParity()
{
System.out.println("Enter your bit value: ");
msg=s.nextLine();
for(i=0;i<msg.length();i++)
{
if(msg.charAt(i)=='1')
{
count++;
}
}
if(count%2==1)
msg='0'+msg;
else
msg='1'+msg;

System.out.println("ans: "+msg);
}
}
class DjParity
{
public static void main(String args[])
{
int ch;
DjParity1 obj=new DjParity1();
Scanner s1=new Scanner(System.in);
System.out.println("Enter your choice: ");
System.out.println("1. even parity: ");
  System.out.println("2. odd parity: ");
ch=s1.nextInt();
switch(ch)
{
case 1:obj.evenParity();
break;
case 2:obj.oddParity();
break;
default:System.out.println("invalid value");
break;
}
}
}

Comments

Popular posts from this blog

Java Program to implement binary checksum

Java Program for block parity

Welcome Guide to Swift 5 Escaping String