📜  在不使用++或+的情况下递增数字

📅  最后修改于: 2021-05-31 19:22:46             🧑  作者: Mango

任务是在不使用++和+运算符的情况下递增数字。

例子:

Input : 3
Output : 4

Input : 9
Output : 10

该想法基于以下事实:负数使用2的补码形式存储。 2的补码形式是通过将位取反然后加1来获得的。因此,如果我们反转给定数字的所有位并应用负号,则会得到数字加1。

C++
// CPP program to increment an unsigned
// int using bitwise operators.
#include 
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


C++
// CPP program to increment an unsigned
// char using bitwise operators.
#include 
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


输出:
4

它也适用于字符。
例子:

Input : a
Output : b

Input : o
Output : p

C++

// CPP program to increment an unsigned
// char using bitwise operators.
#include 
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


输出:
b
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”