/*
* 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
Friday, 5 October 2012
Implementation Of Restoring Algorithm
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();
}
}
Labels:
Binary Division Algorithms,
COA
Location:
Mumbai, Maharashtra, India
Subscribe to:
Posts (Atom)