Related Articles

Related Articles

Increment a number without using ++ or +
  • Difficulty Level : Hard
  • Last Updated : 28 Sep, 2018

The task is to Increment a number without using ++ and + operators.

Examples:

Input : 3
Output : 4

Input : 9
Output : 10

The idea is based on the fact that the negative numbers are stored using 2’s complement form. 2’s complement form is obtained by inverting bits and then adding one. So if we invert all bits of given number and apply negative sign, we get the number plus 1.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to increment an unsigned
// int using bitwise operators.
#include <bits/stdc++.h>
using namespace std;
  
// function that increment the value.
int increment(unsigned int i)
{
    // Invert bits and apply negative sign
    i = -(~i);
  
    return i;
}
  
// Driver code
int main()
{
    int n = 3;
    cout << increment(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to increment 
// an unsigned int using
// bitwise operators.
import java.io.*;
  
class GFG
{
      
// function that increment
// the value.
static long increment(long i)
{
    // Invert bits and 
    // apply negative sign
    i = -(~i);
  
    return i;
}
  
// Driver code
public static void main (String[] args) 
{
    long n = 3;
    System.out.print( increment(n));
}
}
  
// This code is contributed
// by inder_verma.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to increment 
# an unsigned int using 
# bitwise operators.
  
# function that increment the value.
def increment(i):
    # Invert bits and 
    # apply negative sign
    i = -(~i);
  
    return i;
  
# Driver code
if __name__ == "__main__":
    n = 3;
    print(increment(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to increment 
// an unsigned int using
// bitwise operators.
using System;
  
class GFG
{
      
// function that increment
// the value.
static long increment(long i)
{
    // Invert bits and 
    // apply negative sign
    i = -(~i);
  
    return i;
}
  
// Driver code
public static void Main () 
{
    long n = 3;
    Console.WriteLine(increment(n));
}
}
  
// This code is contributed
// by inder_verma.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to increment 
// an unsigned int using 
// bitwise operators.
  
// function that increment the value.
function increment($i)
{
    // Invert bits and 
    // apply negative sign
    $i = -(~$i);
  
    return $i;
}
  
// Driver code
$n = 3;
echo increment($n);
  
// This code is contributed by mits
?>

chevron_right


Output:

4

It also works for characters.
Example:

Input : a
Output : b

Input : o
Output : p

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to increment an unsigned
// char using bitwise operators.
#include <bits/stdc++.h>
using namespace std;
  
// function that increment the value.
char increment(unsigned char i)
{
  
    // Invert bits and apply negative sign
    i = -(~i);
    return i;
}
  
// Driver code
int main()
{
    char n = 'a';
    cout << increment(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to increment 
// an unsigned char using 
// bitwise operators.
class GFG
{
// function that increment the value.
static char increment(char i)
{
  
    // Invert bits and apply
    // negative sign
    int i1 = -(~(int)(i));
    return (char)(i1);
}
  
// Driver code
public static void main(String[] args)
{
    char n = 'a';
    System.out.println(increment(n));
}
}
  
// This code is contributed by mits 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to increment 
# an unsigned char using
# bitwise operators.
  
# function that increment
# the value.
def increment(i):
  
    # Invert bits and
    # apply negative sign
    i = -(~ord(i));
    return chr(i);
  
# Driver code
n = 'a';
print(increment(n));
      
# This code is contributed by mits 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to increment 
// an unsigned char using 
// bitwise operators.
class GFG
{
// function that increment the value.
static char increment(char i)
{
  
    // Invert bits and apply
    // negative sign
    int i1 = -(~(int)(i));
    return (char)(i1);
}
  
// Driver code
static void Main()
{
    char n = 'a';
    System.Console.WriteLine(increment(n));
}
}
  
// This code is contributed by mits 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to increment 
// an unsigned char using
// bitwise operators.
  
// function that increment
// the value.
function increment($i)
{
  
    // Invert bits and
    // apply negative sign
    $i = -(~ord($i));
    return chr($i);
}
  
// Driver code
$n = 'a';
echo increment($n);
      
// This code is contributed by mits 
?>

chevron_right


Output:

b

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :