We know that the negative number is represented in 2’s complement form on most of the architectures. We know that the negative number is represented in 2’s complement form on most of the architectures. The idea is to use bitwise operators. Like addition, Write a function Add() that returns sum of two integers. To subtract 1 from a number x (say 0011001000), flip all the bits after the rightmost 1 bit (we get 001100**1** 111). Finally, flip the rightmost 1 bit also (we get 0011000111) to get the answer. The function should not use any of the arithmetic operators (+, ++, –, -, … etc). Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result. The idea is to use subtractor logic. Write a program to subtract one from a given number. The idea is to use bitwise operators. Like addition, Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, … etc). Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result. The idea is to use subtractor logic. The use of operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–‘ …etc. are not allowed. We have the following lemma hold for 2’s complement representation of signed numbers. We have the following lemma hold for 2’s complement representation of signed numbers.

Say, x is numerical value of a number, then

~x = -(x+1) [ ~ is for bitwise complement ]

Adding 2x on both the sides,

2x + ~x = x – 1

To obtain 2x, left shift x once.

`<?php`

`function`

`subtractOne(`

`$x`

`)`

`{`

` `

`return`

`((`

`$x`

`<< 1) + (~`

`$x`

`));`

`}`

`/* Driver code*/`

`print`

`(subtractOne(13));`

`?>`

**Output:**

12