Search code examples
c#numbersintegerflipinverse

how to get opposite of number in C#?


I'm not sure what the name is for the operation I am attempting, but I would like to "flip" the value of a number within a given range.

I have a C# program that receives a number that varies from any value from 1 to 1023. How would I "flip" the number so that if I receive 1023 then it would be 1, 750 would be 274, and 512 would still be 512 since its exactly half?

I was thinking of using some type of loop but I have never done anything like this. My research came up with people wanting to, for example, turn 40 into 04, but that's not what I am looking for.


Solution

  • If your input number is x then your answer is the expression 1024-x

    In general, if you have numbers in the range of 1 to max, then the answer would be (max+1)-x