Hello I am trying to write a function for my lab which requires me to inverse a positive number with restricted operators. The operators allowed are: ! ~ & ^ | + << >>

My initial approach was to double x using addition: x + x and then subtracting that from x to get the inverse, but was roadblocked due to the operator restrictions. I am rather new to C and trying to learn, if someone could provide me with the ideology behind performing this operation I would greatly appreciate it.

Here is the classic solution you are probably expected to produce:

```
int negate_positive_number(int n) {
return ~n + 1;
}
```

The problem with this code is it only works on 2's complement systems. This is not a real issue as you are probably not going to encounter other architectures today.

Yet the C Standard supports other representations for signed integers:

- sign/magnitude where negative values have the same value bits set but a sign bit set,
- ones' complement where negative values have all bits reversed from the representation of the positive value.

In both of these historical oddities, there are 2 possible representations for `0`

, one of which could be a trap value.

Here is an original solution that works for all 3 architectures: 2's complement, sign/magnitude and ones' complement systems: a bit obscure but more portable:

```
#include <limits.h>
int negate_positive_number(int n) {
return (n ^ INT_MIN ^ INT_MAX) + (1 & (INT_MIN + INT_MAX));
}
```

It uses `+`

, `^`

and `&`

and the definitions from `<limits.h>`

, which might be *off limits*. I assume parentheses are allowed too.

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at

- Prev: Stop objects from sharing values
- Next: How to automatically rebase all children branches onto master after squashing and merging the parent branch?

*1*### How to set column value to last positive integer

*2*### How to inverse a positive integer in C without * or - operators?

*3*### How to negate a positive integer in C without * or - operators?

*4*### How to find the minimum positive integer in a list of lists in O(n)?

*5*### How to use C# Generics to limit method to integer types and still use operators

*6*### Optimization with positive integer parameters

*7*### How can I prevent integer ambiguity when overloading operators in C++

*8*### DocumentFilter for negative and positive integer

*9*### C - Saturating Signed Integer Multiplication with Bitwise Operators

*10*### How to sort string array in C with integer array in parallel? Without structs?

*11*### Matching positive integer with haskell

*12*### How to find occurrences of the largest integer in C without array?

*13*### How does this "if" without operators work?

*14*### Rounding integer division without logical operators

*15*### Finding specific positive integer

*16*### How can ensure that a function only accepts a positive integer?

*17*### How to check if integer is positive or negative in a WHILE loop in Java?

*18*### get positive integer value in objective c

*19*### how to solve the c warning :pointer from integer without a cast?

*20*### Positive Integer in Velocity Template

*21*### How does this "if" without operators work?

*22*### How to find the first positive integer number that is not in the list

*23*### Validating a string as a positive integer

*24*### How can ensure that a function only accepts a positive integer?

*25*### C++ - How to specialise a custom type for std::max without overloading comparison operators?

*26*### How to print 2 characters in C without using logical operators?

*27*### How to convert positive integer to binary representation in the form of a string? Matlab

*28*### how to check if the number is not an integer in C without using isdigit?

*29*### How to get Integer and float input without `scanf()` in c?

## Comments