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

zackmusgrave

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.

chqrlie

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 [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to set column value to last positive integer

From Java

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

From Java

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

From Dev

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

From Dev

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

From Dev

Optimization with positive integer parameters

From Dev

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

From Dev

DocumentFilter for negative and positive integer

From Dev

C - Saturating Signed Integer Multiplication with Bitwise Operators

From Dev

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

From Dev

Matching positive integer with haskell

From Dev

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

From Dev

How does this "if" without operators work?

From Dev

Rounding integer division without logical operators

From Dev

Finding specific positive integer

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

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

From Dev

get positive integer value in objective c

From Dev

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

From Dev

Positive Integer in Velocity Template

From Dev

How does this "if" without operators work?

From Dev

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

From Dev

Validating a string as a positive integer

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

Related Related

  1. 1

    How to set column value to last positive integer

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    Optimization with positive integer parameters

  7. 7

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

  8. 8

    DocumentFilter for negative and positive integer

  9. 9

    C - Saturating Signed Integer Multiplication with Bitwise Operators

  10. 10

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

  11. 11

    Matching positive integer with haskell

  12. 12

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

  13. 13

    How does this "if" without operators work?

  14. 14

    Rounding integer division without logical operators

  15. 15

    Finding specific positive integer

  16. 16

    How can ensure that a function only accepts a positive integer?

  17. 17

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

  18. 18

    get positive integer value in objective c

  19. 19

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

  20. 20

    Positive Integer in Velocity Template

  21. 21

    How does this "if" without operators work?

  22. 22

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

  23. 23

    Validating a string as a positive integer

  24. 24

    How can ensure that a function only accepts a positive integer?

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

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

HotTag

Archive