Professional Documents
Culture Documents
FAL (2022-23) STS3006 TH AP2022232000027 Reference Material I 15-Oct-2022 L-5 Bitwise Algorithms
FAL (2022-23) STS3006 TH AP2022232000027 Reference Material I 15-Oct-2022 L-5 Bitwise Algorithms
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Introduction
to Binary Palindrome
Number = 5
Binary representation = 101
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
What is Binary Palindrome?
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
public class Example { else {
public static void main(String System.out.println("Binary
argc[]) { representation of " + num + " is not
palindrome");
long num = 5, n1;
}
long reverse = 0;
}
n1 = num;
}
while (n1 > 0) {
reverse =reverse << 1;
if ((n1 & 1) == 1)
reverse =reverse ^ 1;
n1 >>= 1;
}
OUTPUT
if(num == reverse) {
System.out.println("Binary The binary representation of 5
representation of " + num + " is is a palindrome
palindrome");
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Euclid's Algorithm
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Euclid's Algorithm
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Euclid's Algorithm
GCD of two numbers is the largest number that divides both of them. A simple way to find
GCD is to factorize both numbers and multiply common prime factors.
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
import java.util.*; a = 35; b = 10;
import java.lang.*; g = gcd(a, b);
class GFG System.out.println("GCD(" + a + " , "
{ + b+ ") = " + g);
// extended Euclidean Algorithm
public static int gcd(int a, int b) a = 31; b = 2;
{ g = gcd(a, b);
if (a == 0) System.out.println("GCD(" + a + " , "
return b; + b+ ") = " + g);
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Booth's Algorithm
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Booth's Algorithm
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Algorithm
Put multiplicand in BR and multiplier in QR
and then the algorithm works as per the following conditions :
1. If Qn and Qn+1 are same i.e. 00 or 11 perform arithmetic shift by 1 bit.
2. If Qn +Qn+1 = 10 do A= A + BR and perform arithmetic shift by 1 bit.
3. If Qn +Qn+1 = 01 do A= A – BR and perform arithmetic shift by 1 bit.
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
// Java code to implement booth's algorithm // function to find the number's complement
class GFG static void complement(int a[], int n)
{ {
int i;
// function to perform adding in the accumulator int[] x = new int[8];
static void add(int ac[], int x[], int qrn) x[0] = 1;
{
int i, c = 0; for (i = 0; i < n; i++)
{
for (i = 0; i < qrn; i++)
a[i] = (a[i] + 1) % 2;
{
}
add(a, x, n);
// updating accumulator with A = A + BR
}
ac[i] = ac[i] + x[i] + c;
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
System.out.print("qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n");
ac[i] = ac[i + 1];
System.out.print("\t\t\tinitial\t\t");
qr[i] = qr[i + 1];
}
display(ac, qr, qrn);
qr[qrn - 1] = temp;
System.out.print("\t\t" + sc + "\n");
}
// function to display operations
while (sc != 0)
static void display(int ac[], int qr[], int qrn)
{
{
System.out.print(qr[0] + "\t" + qn);
int i;
// accumulator content
// SECOND CONDITION
for (i = qrn - 1; i >= 0; i--)
if ((qn + qr[0]) == 1)
{
{
System.out.print(ac[i]);
if (temp == 0)
}
{
System.out.print("\t");
// subtract BR from accumulator
add(ac, mt, qrn);
// multiplier content System.out.print("\t\tA = A - BR\t");
for (i = qrn - 1; i >= 0; i--)
{ for (int i = qrn - 1; i >= 0; i--)
System.out.print(qr[i]); {
} System.out.print(ac[i]);
} }
// Function to implement booth's algo temp = 1;
static void boothAlgorithm(int br[], int qr[], int mt[], }
int qrn, int sc) // THIRD CONDITION
{ else if (temp == 1)
int qn = 0; {
int[] ac = new int[10]; // add BR to accumulator
int temp = 0; add(ac, br, qrn);
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
System.out.print("\t\tA = A + BR\t");
for (int i = qrn - 1; i >= 0; i--) // Driver code
{ public static void main(String[] args)
System.out.print(ac[i]); {
} int[] mt = new int[10];
temp = 0; int sc;
} int brn, qrn;
System.out.print("\n\t");
rightShift(ac, qr, qn, qrn); // Number of multiplicand bit
} brn = 4;
// FIRST CONDITION // multiplicand
else if (qn - qr[0] == 0) int br[] = {0, 1, 1, 0};
{
rightShift(ac, qr, qn, qrn); // copy multiplier to temp array mt[]
} for (int i = brn - 1; i >= 0; i--)
display(ac, qr, qrn); {
System.out.print("\t"); mt[i] = br[i];
// decrement counter }
sc--;
System.out.print("\t" + sc + "\n"); reverse(br);
}
} complement(mt, brn);
static void reverse(int a[]) // No. of multiplier bit
{ qrn = 4;
int i, k, n = a.length; // sequence counter
int t; sc = qrn;
for (i = 0; i < n / 2; i++)
{ // multiplier
t = a[i]; int qr[] = {1, 0, 1, 0};
a[i] = a[n - i - 1]; reverse(qr);
a[n - i - 1] = t;
boothAlgorithm(br, qr, mt, qrn, sc);
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
System.out.print("\n"
+ "Result = ");
Output :
qn q[n + 1] BR AC QR sc
initial 0000 1010 4
0 0 rightShift 0000 0101 3
1 0 A = A - BR 1010
rightShift 1101 0010 2
0 1 A = A + BR 0011
rightShift 0001 1001 1
1 0 A = A - BR 1011
rightShift 1101 1100 0
Result = 1100
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank You …
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.