Implementation Of Restoring Algorithm on 4 Bit Numbers
package addandshiftalgo;
import java.util.Scanner;
/**
*
* @author Naved Momin<naved.spartans.rocks75@gmail.com>
*/
public class RestoringDivision {
int m , q, c, ac;
String acStr = "0", qstr, mstr, cStr, acc, twoscompliment;
private void read() {
Scanner s = new Scanner(System.in);
System.out.println( "enter m " ) ;
int read = s.nextInt();
m = read;
System.out.println( "enter q " ) ;
read = s.nextInt();
q = read;
twoscompliment();
}
void twoscompliment( ){
twoscompliment = Integer.toBinaryString( - m ).substring(27, 32);
}
void startALgo( ){
read();
for (int i = 0; i < 4; i++) {
qstr = Integer.toBinaryString(q);
mkQOf4Bit();
char charAt0 = qstr.charAt(0);
acStr = Integer.toBinaryString(ac);
acStr = acStr + String.valueOf(charAt0);
ac = Integer.parseInt(acStr,2);
q <<= 1;
qstr = Integer.toBinaryString(q);
if( qstr.length() > 4){
qstr = qstr.substring(1);
}else{
q = Integer.parseInt(qstr, 2);
}
int n = Integer.parseInt(twoscompliment, 2) ;
n = n + ac;
String bString = Integer.toBinaryString(n);
acStr = bString.substring(1);
ac = Integer.parseInt(acStr, 2);
cStr = String.valueOf( acStr.charAt(0) ) ;
c = Integer.parseInt(cStr,2);
if( c == 1){
qstr = Integer.toBinaryString(q);
mkQOf4Bit();
if( qstr.length() > 4 )
qstr = qstr.substring(1);
String substring = qstr.substring(0, 3);
qstr = substring + "0";
//qstr = qstr.substring(1);
q = Integer.parseInt(qstr,2);
String sum = Integer.toBinaryString(c) + Integer.toBinaryString(ac);
int s = Integer.parseInt(sum, 2);
s = s + m;
sum = Integer.toBinaryString(s);
ac = Integer.parseInt(sum.substring(1),2);
c = 0;
}else{
qstr = Integer.toBinaryString(q);
mkQOf4Bit();
if( qstr.length() > 4 )
qstr = qstr.substring(1);
String substring = qstr.substring(0, 3);
qstr = substring + "1";
q = Integer.parseInt(qstr,2);
}
}
System.out.println( "ac = " + ac + " q = " + q );
}
void mkQOf4Bit( ){
if( qstr.length() <= 4 ){
int formula = 4 - qstr.length();
for (int j = 0; j < formula; j++) {
qstr = "0" + qstr;
}
}
}
public static void main(String[] args) {
// TODO code application logic here
new RestoringDivision().startALgo();
}
}
No comments:
Post a Comment