/*
* 4 bit Non Restoring Division implementation
*/
package addandshiftalgo;
import java.util.Scanner;
/**
*
* @author Naved Momin
*/
public class NonRestoringDivision {
int m , q, c, ac;
String acStr = "0000", 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) ).substring(1);
ac = Integer.parseInt(acStr,2);
qstr = Integer.toBinaryString(q);
mkQOf4Bit();
qstr = qstr.substring(1);
q = Integer.parseInt(qstr, 2);
q <<= 1;
qstr = Integer.toBinaryString(q);
mkQOf4Bit();
if( c == 1){
ac = ac + m;
acStr = Integer.toBinaryString(ac);
if( acStr.length() > 5){
acStr = acStr.substring(1);
}
charAt0 = acStr.charAt(0);
c = Integer.parseInt(String.valueOf(charAt0), 2);
if( c == 0 ){
String substring = qstr.substring(0, 3);
qstr = substring + "1";
q = Integer.parseInt(qstr, 2);
}else if( c == 1){
String substring = qstr.substring(0, 3);
qstr = substring + "0";
q = Integer.parseInt(qstr, 2);
}
acStr = acStr.substring(1);
}else{
int twoscomp = Integer.parseInt(twoscompliment, 2);
ac = ac + twoscomp;
acStr = Integer.toBinaryString(ac);
if( acStr.length() > 5){
acStr = acStr.substring(1);
}
charAt0 = acStr.charAt(0);
c = Integer.parseInt(String.valueOf(charAt0), 2);
if( c == 1){
String substring = qstr.substring(0, 3);
qstr = substring + "0";
q = Integer.parseInt(qstr, 2);
}else if( c == 0 ){
String substring = qstr.substring(0, 3);
qstr = substring + "1";
q = Integer.parseInt(qstr, 2);
}
acStr = acStr.substring(1);
}
}
ac = Integer.parseInt(acStr, 2);
if( c == 1){
String acc = Integer.toBinaryString(c) + acStr;
int accInt = Integer.parseInt(acc, 2);
ac = accInt + m;
acStr = Integer.toBinaryString(ac);
if( acStr.length() > 5 ){
acStr = acStr.substring(1);
}
ac = Integer.parseInt(acStr, 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 NonRestoringDivision().startALgo();
}
}
Friday, 12 October 2012
Non Restroing Division
Labels:
Binary Division Algorithms,
COA
Location:
Mumbai, Maharashtra, India
Subscribe to:
Post Comments (Atom)
I believe that you did smart resolution when you came up with this subject of the article of yours over here. Do you mostly compose your entries by yourself or maybe you work with a business partner or even a helper?
ReplyDeleteno i compose every thing by myself.
Deletefrom the " optimal solution to any problem " to spelling mistake's in my post's, i am the man behind it. :)