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

zackmusgrave

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

My initial approach was to double x using addition: x + x and then subtracting that from x to get the opposite value, 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 idea 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 inverse a positive integer in C without * or - operators?

From Java

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

From Dev

Rounding integer division without logical operators

From Dev

Negate integer Javascript

From Dev

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

From Dev

get positive integer value in objective c

From Java

How to set column value to last positive integer

From Dev

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

From Dev

C - Saturating Signed Integer Multiplication with Bitwise Operators

From Dev

How does this "if" without operators work?

From Dev

How does this "if" without operators work?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

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?

From Java

How to negate 'isblank' function

From Dev

How To Negate A Boolean In A Pipeline?

From Dev

How to negate countif

From Dev

How to negate a variable in groovy

From Dev

How to negate backreference regex

From Dev

How to negate a literal string

From Dev

How to negate a number in IJVM?

From Dev

How to negate a string in regex

From Dev

how to negate conditional in Java?

From Dev

How to negate a case pattern

From Dev

Verilog - how to negate an array?

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Rounding integer division without logical operators

  4. 4

    Negate integer Javascript

  5. 5

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

  6. 6

    get positive integer value in objective c

  7. 7

    How to set column value to last positive integer

  8. 8

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

  9. 9

    C - Saturating Signed Integer Multiplication with Bitwise Operators

  10. 10

    How does this "if" without operators work?

  11. 11

    How does this "if" without operators work?

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

    How to negate 'isblank' function

  20. 20

    How To Negate A Boolean In A Pipeline?

  21. 21

    How to negate countif

  22. 22

    How to negate a variable in groovy

  23. 23

    How to negate backreference regex

  24. 24

    How to negate a literal string

  25. 25

    How to negate a number in IJVM?

  26. 26

    How to negate a string in regex

  27. 27

    how to negate conditional in Java?

  28. 28

    How to negate a case pattern

  29. 29

    Verilog - how to negate an array?

HotTag

Archive