Search Logic Blocks

Wednesday, August 5, 2020

Java: Adding Binary Numbers

We already discussed about the binary numbers and also how to convert the binary numbers to the decimal numbers and convert the decimal numbers to the binary numbers. Let's talk about how to add binary numbers.

We know that binary numbers have only 0s and 1s, and when we add two or more binary numbers, the result should be same as the result of their respective decimal numbers.

How bits are added


0 0 1
+ 0 + 1 + 1
0 1 1 0

0 + 0 = 0 (0)
0 + 1 = 1 (1)
1 + 1 = 2 (10)

And when there are more bits, the binary addition will look like this -

When two 1s are added, result is 10 and 1 is carried over to left.


1 <-
1 0 1 0 1 1
+ 0 + 1 + 1
1 0 1 1 1 0 0

2  + 0 = 2 (10)
2 + 1 = 3 (11)
3 + 1 = 4 (100)

Let's try to add two binary numbers - 28 (11100) and 43 (101011) and result should be 71 (1000111)

28 + 43 = 71


1 1 <-
1 0 1 0 1 1
+ 1 1 1 0 0
1 0 0 0 1 1 1

Now, let's write a program for binary addition - I have used two methods binaryToDecimal() and calcPower() same as in the last post convert the binary numbers to the decimal numbers and convert the decimal numbers to the binary numbers
public static int binaryToDecimal(Long binaryVal)
public static int calcPower(int no1, int no2)
And also added one more method to add two binary numbers - addBinaryNumbers()
public static Long addBinaryNumbers(Long binary1, Long binary2)
I have used the same logic as explained above -
  1. The program will read both the binary numbers bit by bit from right to left in a while loop, till all bits are done.
  2. Add both bits, and also add carryover bit if there is any.
  3. Add the result bit to convert result into Long binary number again.
  4. If in last, there is carryover left, add that to the result too.
import java.util.Scanner;

public class AddBinaryNumbers {
public static void main(String args[]) {
System.out.println("Enter two binary values");
Scanner sc = new Scanner(System.in);
Long binary1 = sc.nextLong();
Long binary2 = sc.nextLong();

addBinaryNumbers(binary1, binary2);
}

public static int binaryToDecimal(Long binaryVal) {
int decVal = 0;
int rem, pow = 0;

while(binaryVal != 0) {
rem = (int)(binaryVal % 10);
decVal = decVal + rem * calcPower(2, pow);
binaryVal = binaryVal / 10;
pow++;
}
return decVal;
}

public static int calcPower(int no1, int no2) {
int powVal = 1;
for(int i = 0; i < no2; i++) {
powVal = powVal * no1;
}
return powVal;
}

public static Long addBinaryNumbers(Long binary1, Long binary2) {
Long binaryVal = 0L;

int sum = 0;
int rem1, rem2, remainder = 0, i = 0, pow = 0;

int decVal1 = binaryToDecimal(binary1);
int decVal2 = binaryToDecimal(binary2);

System.out.println("Binary number 1 => " + binary1 + " : " + decVal1);
System.out.println("Binary number 2 => " + binary2 + " : " + decVal2);

// Addition is done bit by bit
while(binary1 != 0 || binary2 != 0) {
// rem1 is next bit of binary1 and rem2 is next bit of binary2
rem1 = (int)(binary1 % 10);
rem2 = (int)(binary2 % 10);

// remaining bits of binary numbers
binary1 = binary1 / 10;
binary2 = binary2 / 10;

// sum of the bits + carryover if any
sum = (rem1 + rem2 + remainder) % 2; // remainder is carry over
remainder = (rem1 + rem2 + remainder) / 2;
binaryVal = binaryVal + sum * calcPower(10, pow);
pow++;
}
if(remainder != 0) {
// If last bit addition has carryover, add it too
binaryVal = binaryVal + calcPower(10, pow);
}

int totVal = binaryToDecimal(binaryVal);

System.out.println("Sum of two binary numbers => " + binaryVal + " : " + totVal);
return binaryVal;
}
}
Here is the output:

Enter two binary values
11100
101011
Binary number 1 => 11100 : 28
Binary number 2 => 101011 : 43
Sum of two binary numbers => 1000111 : 71

Enter two binary values
111111111
111111111
Binary number 1 => 111111111 : 511
Binary number 2 => 111111111 : 511
Sum of two binary numbers => 1111111110 : 1022

NOTE: This example is good for till 9 bits binary numbers as according to Long data type values range.

The code can be accessed here: Github Link

Saturday, August 1, 2020

Java: Inner Classes

An inner class is a nested class, means a class within a class. Inner class is like other member variables of outer class and can access all the private, public and protected members of outer class. Inner class is used when the class is to be used only by the outer class and not need to be accessed by other classes. One example is a vehicle and wheel. A wheel is nothing without any vehicle, it has to be used only with the vehicle. Here is the example of Wheel class in Vehicle class -
public class Vehicle {
private String vehicleType;
private String vehicleColor;
private int noOfWheels = 0;

// Class Wheel Starts
class Wheel {
String wheelType;
int wheelSize;

Wheel(String type, int size) {
wheelType = type;
wheelSize = size;
noOfWheels++;
}

String getType() {
return this.wheelType;
}

void setType(String type) {
this.wheelType = type;
}

int getSize() {
return this.wheelSize;
}

void setSize(int size) {
this.wheelSize = size;
}
}
// Class Wheel Ends

Vehicle(String type, String color) {
vehicleType = type;
vehicleColor = color;
}

String getType() {
return this.vehicleType;
}

String getColor() {
return this.vehicleColor;
}

void setType(String type) {
this.vehicleType = type;
}

void setColor(String color) {
this.vehicleColor = color;
}

int getNoOfWheels() {
return this.noOfWheels;
}
}
Vehicle class has the fields - vehicle type, vehicle color, no of wheels and a wheel class member. (You can add more features - this is just for example) The Vehicle class constructor initializes vehicle type and vehicle color. There are getter and setter methods for vehicle type and vehicle color, and getter method for no of wheels.

Member class Wheel has the fields - wheel type and wheel size. The Wheel class constructor initializes wheel type and wheel color. And also accesses the field no of wheels from outer class Vehicle and increments it by 1. So, whenever a new wheel is added to the vehicle, it will increase the no of wheels. 

Here is the class VehicleDemo, which creates an instance of class Vehicle and then creates 4 instances of Wheel class for the vehicle.
public class VehicleDemo {
public static void main(String args[]) {
Vehicle v = new Vehicle("Truck", "red");
System.out.println("Type of Vehicle is: " + v.getType());
System.out.println("Color of Vehicle is: " + v.getColor());
Vehicle.Wheel wh1 = v.new Wheel("thick", 28);
Vehicle.Wheel wh2 = v.new Wheel("thick", 28);
System.out.println("No Of Wheels: " + v.getNoOfWheels());
Vehicle.Wheel wh3 = v.new Wheel("thin", 15);
Vehicle.Wheel wh4 = v.new Wheel("thin", 15);
System.out.println("No Of Wheels: " + v.getNoOfWheels());
}
}
Here is the output:

Type of Vehicle is: Truck
Color of Vehicle is: red
No Of Wheels: 2
No Of Wheels: 4

Please pay close attention how the inner class object is created. 
Vehicle v = new Vehicle("Truck", "red");
Vehicle.Wheel wh1 = v.new Wheel("thick", 28);
Note: Inner classes are very useful in cases when inner class is related to outer class, and there is no use of the inner class outside from the outer class.

Code can be accessed here: Github Link