算法设计:如何实现不带算术运算符的减法1

2021年3月13日16:59:36 发表评论 860 次浏览

本文概述

编写一个程序, 从给定的数字中减去一个。不允许使用" +", "-", " *", " /", " ++", " –"等运算符。

例子:

Input:  12
Output: 11

Input:  6
Output: 5

方法1

要从数字x中减去1(例如0011001000), 请翻转最右边1位之后的所有位(我们得到001100

1

111)。最后, 也将最右边的1位翻转(我们得到0011000111)以获得答案。

C

// C code to subtract
// one from a given number
#include <stdio.h>
  
int subtractOne( int x)
{
     int m = 1;
  
     // Flip all the set bits
     // until we find a 1
     while (!(x & m)) {
         x = x ^ m;
         m <<= 1;
     }
  
     // flip the rightmost 1 bit
     x = x ^ m;
     return x;
}
  
/* Driver program to test above functions*/
int main()
{
     printf ( "%d" , subtractOne(13));
     return 0;
}

Java

// Java code to subtract
// one from a given number
import java.io.*;
  
class GFG 
{
static int subtractOne( int x)
{
     int m = 1 ;
  
     // Flip all the set bits
     // until we find a 1
     while (!((x & m) > 0 )) 
     {
         x = x ^ m;
         m <<= 1 ;
     }
  
     // flip the rightmost
     // 1 bit
     x = x ^ m;
     return x;
}
  
// Driver Code
public static void main (String[] args) 
{
     System.out.println(subtractOne( 13 ));
}
}
  
// This code is contributed
// by anuj_67.

Python3

# Python 3 code to subtract one from 
# a given number
def subtractOne(x):
     m = 1
  
     # Flip all the set bits
     # until we find a 1
     while ((x & m) = = False ):
         x = x ^ m
         m = m << 1
      
     # flip the rightmost 1 bit
     x = x ^ m
     return x
  
# Driver Code
if __name__ = = '__main__' :
     print (subtractOne( 13 ))
      
# This code is contributed by
# Surendra_Gangwar

C#

// C# code to subtract
// one from a given number
using System;
  
class GFG 
{
static int subtractOne( int x)
{
     int m = 1;
  
     // Flip all the set bits
     // until we find a 1
     while (!((x & m) > 0)) 
     {
         x = x ^ m;
         m <<= 1;
     }
  
     // flip the rightmost
     // 1 bit
     x = x ^ m;
     return x;
}
  
// Driver Code
public static void Main () 
{
     Console.WriteLine(subtractOne(13));
}
}
  
// This code is contributed
// by anuj_67.

的PHP

<?php
// PHP code to subtract
// one from a given number
  
function subtractOne( $x )
{
     $m = 1;
  
     // Flip all the set bits
     // until we find a 1
     while (!( $x & $m ))
     {
         $x = $x ^ $m ;
         $m <<= 1;
     }
  
     // flip the
     // rightmost 1 bit
     $x = $x ^ $m ;
     return $x ;
}
  
// Driver Code
     echo subtractOne(13);
      
// This code is contributed
// by anuj_67.
?>

输出如下:

12

方法2(如果允许+)

我们知道, 在大多数架构中, 负数都以2的补码形式表示。对于带符号的数字的2的补码表示, 我们具有以下引理。

假设x是数字的数值, 则

〜x =-(x + 1)[〜用于按位补码]

两侧加2倍,

2x +〜x = x – 1

要获得2x, 请向左移x一次。

C ++

#include <stdio.h>
  
int subtractOne( int x)
{
     return ((x << 1) + (~x));
}
  
/* Driver program to test above functions*/
int main()
{
     printf ( "%d" , subtractOne(13));
     return 0;
}

Java

class GFG 
{
  
     static int subtractOne( int x) 
     {
         return ((x << 1 ) + (~x));
     }
  
     /* Driver code*/
     public static void main(String[] args) 
     {
         System.out.printf( "%d" , subtractOne( 13 ));
     }
}
  
// This code has been contributed by 29AjayKumar

Python3

def subtractOne(x):
  
     return ((x << 1 ) + (~x));
  
# Driver code
print (subtractOne( 13 ));
  
# This code is contributed by mits

C#

using System;
      
class GFG 
{
  
     static int subtractOne( int x) 
     {
         return ((x << 1) + (~x));
     }
  
     /* Driver code*/
     public static void Main(String[] args) 
     {
         Console.Write( "{0}" , subtractOne(13));
     }
}
  
// This code contributed by Rajput-Ji

的PHP

<?php
function subtractOne( $x ) 
{
     return (( $x << 1) + (~ $x ));
}
  
/* Driver code*/
  
print (subtractOne(13));
  
// This code has been contributed by mits
?>

输出如下:

12

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: