This is the java program to perform addition of two numbers without using any arithmetic operators. The summation of two numbers can be obtained using XOR operation and carry can be obtained using AND performed at bit level.
Here is the source code of the Java Program to Perform Addition Operation Using Bit-wise Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is sample program to perform addition operation using bitwise operators. import java.util.Scanner; public class Bitwise_Addition { static int add(int x, int y) { int carry; while(y!=0) { carry = x & y; x = x ^ y; y = carry << 1; } return x; } public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Enter the numbers to be added:"); int x = input.nextInt(); int y = input.nextInt(); System.out.println("The Summation is: "+add(x, y)); input.close(); } }
Output:
$ javac Bitwise_Addition.java $ java Bitwise_Addition Enter the numbers to be added: 15 16 The Summation is: 31
Related posts:
File Upload with Spring MVC
Hướng dẫn Java Design Pattern – Observer
Convert char to String in Java
Handling Errors in Spring WebFlux
Using JWT with Spring Security OAuth
How To Serialize and Deserialize Enums with Jackson
Java Program to implement Priority Queue
Java – Reader to InputStream
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Setting Up Swagger 2 with a Spring REST API
Java Program to Implement Stack API
Introduction to Thread Pools in Java
Overview of Spring Boot Dev Tools
New Features in Java 13
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to Implement the Program Used in grep/egrep/fgrep
Working with Network Interfaces in Java
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Handling URL Encoded Form Data in Spring REST
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Use rand and srand Functions
Simplify the DAO with Spring and Java Generics
Java InputStream to Byte Array and ByteBuffer
Serverless Functions with Spring Cloud Function
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Spring Security with Maven
Java Program to Represent Graph Using 2D Arrays
The Spring @Controller and @RestController Annotations
Iterating over Enum Values in Java
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Implement AVL Tree
Guide to Java 8 groupingBy Collector