Incrementa un número sin usar ++ o +

La tarea es Incrementar un número sin usar los operadores ++ y +.
Ejemplos: 
 

Input : 3
Output : 4

Input : 9
Output : 10

La idea se basa en el hecho de que los números negativos se almacenan en forma de complemento a 2 . La forma de complemento a 2 se obtiene invirtiendo bits y luego sumando uno. Entonces, si invertimos todos los bits del número dado y aplicamos el signo negativo, obtenemos el número más 1. 
 

C++

// 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;
}

Java

// 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.

Python3

# 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

C#

// 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.

PHP

<?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
?>

Javascript

<script>
// Javascript 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
 
let n = 3;
document.write(increment(n));
 
 
// This code is contributed by _saurabh_jaiswal
 
</script>
Producción

4

También funciona para personajes. 
Ejemplo: 
 

Input : a
Output : b

Input : o
Output : p

C++

// 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;
}

Java

// 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

Python3

# 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

C#

// 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

PHP

<?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
?>

Javascript

<script>
    // Javascript program to increment
    // an unsigned char using
    // bitwise operators.
     
    // function that increment the value.
    function increment(i)
    {
 
        // Invert bits and apply
        // negative sign
        let i1 = -(~(i).charCodeAt());
        return String.fromCharCode(i1);
    }
     
    let n = 'a';
    document.write(increment(n));
 
// This code is contributed by mukesh07.
</script>
Producción

b

Similar al enfoque de usar el complemento, podemos usar la doble negación para incrementar un número, ya que -(-1) es equivalente a + 1.

C++

// CPP program to increment an unsigned
// int using double negation
 
#include <bits/stdc++.h>
using namespace std;
 
// function that increment the value.
int increment(unsigned int i)
{
    // Using double negation
    i = i - (-1);
 
    return i;
}
 
// Driver code
int main()
{
    int n = 3;
    cout << increment(n);
    return 0;
}
 
 
//This code is contributed by phasing17

Javascript

// JavaScript program to increment an unsigned
// int using double negation
 
// function that increment the value.
function increment(i)
{
    // Using double negation
    i = i - (-1);
 
    return i;
}
 
// Driver code
let n = 3;
console.log(increment(n));
 
// This code is contributed by phasing17
Producción

4

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *