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 set column value to last positive integer

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 Java

How to negate 'isblank' function

From Dev

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

From Dev

Negate integer Javascript

From Dev

How to negate countif

From Dev

How to negate a literal string

From Dev

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

From Dev

How to negate backreference regex

From Dev

How to negate a variable in groovy

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

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

From Dev

How does this "if" without operators work?

From Dev

How to negate a string in regex

From Dev

Rounding integer division without logical operators

From Dev

How To Negate A Boolean In A Pipeline?

From Dev

How to negate a number in IJVM?

From Dev

how to negate conditional 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

How to negate a case pattern

From Dev

Verilog - how to negate an array?

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

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

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 inverse a positive integer in C without * or - operators?

  4. 4

    How to negate 'isblank' function

  5. 5

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

  6. 6

    Negate integer Javascript

  7. 7

    How to negate countif

  8. 8

    How to negate a literal string

  9. 9

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

  10. 10

    How to negate backreference regex

  11. 11

    How to negate a variable in groovy

  12. 12

    C - Saturating Signed Integer Multiplication with Bitwise Operators

  13. 13

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

  14. 14

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

  15. 15

    How does this "if" without operators work?

  16. 16

    How to negate a string in regex

  17. 17

    Rounding integer division without logical operators

  18. 18

    How To Negate A Boolean In A Pipeline?

  19. 19

    How to negate a number in IJVM?

  20. 20

    how to negate conditional in Java?

  21. 21

    get positive integer value in objective c

  22. 22

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

  23. 23

    How to negate a case pattern

  24. 24

    Verilog - how to negate an array?

  25. 25

    How does this "if" without operators work?

  26. 26

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

  27. 27

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

  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