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:
Java Program to Solve Tower of Hanoi Problem using Stacks
Custom Exception trong Java
Toán tử trong java
Send email with SMTPS (eg. Google GMail)
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement LinkedHashMap API
Shuffling Collections In Java
Returning Image/Media Data with Spring MVC
Java equals() and hashCode() Contracts
Java Program to Convert a Decimal Number to Binary Number using Stacks
Multipart Upload with HttpClient 4
Quick Guide to java.lang.System
Quick Guide to the Java StringTokenizer
Giới thiệu Google Guice – Injection, Scope
Spring Boot - Introduction
A Guide to @RepeatedTest in Junit 5
Java Program to Implement Self organizing List
Find the Registered Spring Security Filters
Giới thiệu Json Web Token (JWT)
Custom HTTP Header with the HttpClient
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Difference Between Wait and Sleep in Java
Inheritance with Jackson
Template Engines for Spring
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Implement Shell Sort
Spring Boot - Cloud Configuration Server
Hướng dẫn sử dụng Printing Service trong Java
Jackson – Unmarshall to Collection/Array
Get the workstation name or IP
Zipping Collections in Java