Add one to given number
In this article, we will explore a simple programs that adds one to a given number. We will start with the problem statement and then provide a detailed explanation of the code with suitable examples. Additionally, we will present the standard pseudocode and algorithm for the solution, followed by an explanation of the program's output. Finally, we will analyze the time complexity of the code.
Problem Statement
The problem is to design a program that takes an integer as input and increments it by one using bitwise operators without using the conventional addition operator (+). The goal is to efficiently add one to the given number and print the resulting value.
Explanation with Examples
Let's understand the concept with some examples:
-
Adding One to 17 (Decimal): Binary representation of 17: 10001 Binary representation of (-(~17)): 10010 (18 in decimal)
-
Adding One to 9 (Decimal): Binary representation of 9: 1001 Binary representation of (-(~9)): 1010 (10 in decimal)
-
Adding One to 1 (Decimal): Binary representation of 1: 1 Binary representation of (-(~1)): 10 (2 in decimal)
-
Adding One to 7 (Decimal): Binary representation of 7: 111 Binary representation of (-(~7)): 1000 (8 in decimal)
-
Adding One to 10 (Decimal): Binary representation of 10: 1010 Binary representation of (-(~10)): 1011 (11 in decimal)
-
Adding One to -1 (Decimal): Binary representation of -1: 11111111 (assuming 8-bit representation, two's complement) Binary representation of (-(~-1)): 0 (0 in decimal)
Pseudocode
function incrementByOne(num)
return -(~num)
Algorithm Explanation: The algorithm uses two bitwise operators to add one to the given number: the bitwise NOT operator (~) and the bitwise negation operator (-). Here's the step-by-step explanation:
- Invert all the bits of the input number using the bitwise NOT operator (~). This is equivalent to taking the one's complement of the number.
- Negate the result obtained in step 1 using the negation operator (-). This is equivalent to taking the two's complement of the number.
- The final result is the incremented value of the given number.
Code Solution
// C Program
// Add one to given number
#include <stdio.h>
// Increase given number by one
void incrementByOne(int num)
{
// Display given number
printf("\n Number : %d",num);
// Display increment value by one
printf("\n After Add One : %d", -(~num));
}
int main()
{
// Test Cases
// 17 (10001) => (10010)
incrementByOne(17);
// 9 (1001) => (1010)
incrementByOne(9);
// 1 (1) => (10)
incrementByOne(1);
// 7 (111) => (1000)
incrementByOne(7);
// 10 (1010) => (1011)
incrementByOne(10);
// -1 (11111111111) => (0)
incrementByOne(-1);
return 0;
}
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
/*
Java Program
Add one to given number
*/
public class Incrementation
{
// Increase given number by one
public void incrementByOne(int num)
{
// Display given number
System.out.print("\n Number : " + num);
// Display increment value by one
System.out.print("\n After Add One : " + -(~num));
}
public static void main(String[] args)
{
Incrementation task = new Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
}
}
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Add one to given number
*/
class Incrementation
{
public:
// Increase given number by one
void incrementByOne(int num)
{
// Display given number
cout << "\n Number : " << num;
// Display increment value by one
cout << "\n After Add One : " << -(~num);
}
};
int main()
{
Incrementation task = Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
return 0;
}
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
// Include namespace system
using System;
/*
C# Program
Add one to given number
*/
public class Incrementation
{
// Increase given number by one
public void incrementByOne(int num)
{
// Display given number
Console.Write("\n Number : " + num);
// Display increment value by one
Console.Write("\n After Add One : " + -(~num));
}
public static void Main(String[] args)
{
Incrementation task = new Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
}
}
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
<?php
/*
Php Program
Add one to given number
*/
class Incrementation
{
// Increase given number by one
public function incrementByOne($num)
{
// Display given number
echo "\n Number : ". $num;
// Display increment value by one
echo "\n After Add One : ". -(~$num);
}
}
function main()
{
$task = new Incrementation();
// Test Cases
// 17 (10001) => (10010)
$task->incrementByOne(17);
// 9 (1001) => (1010)
$task->incrementByOne(9);
// 1 (1) => (10)
$task->incrementByOne(1);
// 7 (111) => (1000)
$task->incrementByOne(7);
// 10 (1010) => (1011)
$task->incrementByOne(10);
// -1 (11111111111) => (0)
$task->incrementByOne(-1);
}
main();
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
/*
Node Js Program
Add one to given number
*/
class Incrementation
{
// Increase given number by one
incrementByOne(num)
{
// Display given number
process.stdout.write("\n Number : " + num);
// Display increment value by one
process.stdout.write("\n After Add One : " + (-(~num)));
}
}
function main()
{
var task = new Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
}
main();
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
# Python 3 Program
# Add one to given number
class Incrementation :
# Increase given number by one
def incrementByOne(self, num) :
# Display given number
print("\n Number : ", num, end = "")
# Display increment value by one
print("\n After Add One : ", -(~num), end = "")
def main() :
task = Incrementation()
# Test Cases
# 17 (10001) => (10010)
task.incrementByOne(17)
# 9 (1001) => (1010)
task.incrementByOne(9)
# 1 (1) => (10)
task.incrementByOne(1)
# 7 (111) => (1000)
task.incrementByOne(7)
# 10 (1010) => (1011)
task.incrementByOne(10)
# -1 (11111111111) => (0)
task.incrementByOne(-1)
if __name__ == "__main__": main()
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
# Ruby Program
# Add one to given number
class Incrementation
# Increase given number by one
def incrementByOne(num)
# Display given number
print("\n Number : ", num)
# Display increment value by one
print("\n After Add One : ", -(~num))
end
end
def main()
task = Incrementation.new()
# Test Cases
# 17 (10001) => (10010)
task.incrementByOne(17)
# 9 (1001) => (1010)
task.incrementByOne(9)
# 1 (1) => (10)
task.incrementByOne(1)
# 7 (111) => (1000)
task.incrementByOne(7)
# 10 (1010) => (1011)
task.incrementByOne(10)
# -1 (11111111111) => (0)
task.incrementByOne(-1)
end
main()
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
/*
Scala Program
Add one to given number
*/
class Incrementation
{
// Increase given number by one
def incrementByOne(num: Int): Unit = {
// Display given number
print("\n Number : " + num);
// Display increment value by one
print("\n After Add One : " + -(~num));
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Incrementation = new Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
}
}
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
/*
Swift 4 Program
Add one to given number
*/
class Incrementation
{
// Increase given number by one
func incrementByOne(_ num: Int)
{
// Display given number
print("\n Number : ", num, terminator: "");
// Display increment value by one
print("\n After Add One : ", -(~num), terminator: "");
}
}
func main()
{
let task: Incrementation = Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
}
main();
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
/*
Kotlin Program
Add one to given number
*/
class Incrementation
{
// Increase given number by one
fun incrementByOne(num: Int): Unit
{
// Display given number
print("\n Number : " + num);
// Display increment value by one
print("\n After Add One : " + -(num.inv()));
}
}
fun main(args: Array < String > ): Unit
{
var task: Incrementation = Incrementation();
// Test Cases
// 17 (10001) => (10010)
task.incrementByOne(17);
// 9 (1001) => (1010)
task.incrementByOne(9);
// 1 (1) => (10)
task.incrementByOne(1);
// 7 (111) => (1000)
task.incrementByOne(7);
// 10 (1010) => (1011)
task.incrementByOne(10);
// -1 (11111111111) => (0)
task.incrementByOne(-1);
}
Output
Number : 17
After Add One : 18
Number : 9
After Add One : 10
Number : 1
After Add One : 2
Number : 7
After Add One : 8
Number : 10
After Add One : 11
Number : -1
After Add One : 0
Program Output Explanation
The C program will display the original number and its incremented value for each test case, as shown in the output section of the provided code.
Time Complexity Analysis
The time complexity of the given program is constant O(1). Regardless of the input value, the algorithm performs a fixed number of bitwise operations to calculate the result. Therefore, the time taken to execute the program does not depend on the input size, resulting in a constant time complexity.
Finally
In this article, we discussed the problem of adding one to a given number using bitwise operators in C. We provided suitable examples to explain the concept and presented the standard pseudocode and algorithm used in the program. Additionally, we analyzed the time complexity, which turned out to be constant O(1). Understanding this bitwise operation can be useful in scenarios where conventional arithmetic operations are not allowed or need to be optimized for performance.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment