Asking The Stack XD

So, I’m sitting around at work and chiseling away at some C# code for a quick little compatibility routine I got.  Yeah, I’m bored. =^-^=

Anyway, the way this device throws data back is as a simple unsigned word value.  Well, almost simple.  It actually does include values starting at 0xFFFF for the value of -1 and going down from there. (>.>)

Did that make any sense? XD

I figured I’d use the KISS method to turn this friendly little mess into a signed integer.   My little snippet of code is as seen here:

int GotSlope = gotData[0] + (gotData[1] << 8);
if (GotSlope > 0x8000) GotSlope = -(0xFFFF – GotSlope);

It does the job. =^-^=

But, as usual, I get curious.  I wonder if there are more readable and/or efficient ways of doing it.  I Google “positive to negative in C#” and one of my top results leads to my favourite site… StackOverflow.com! =^-^=

This is where the LOLs start coming in.  Someone did legitimately ask this question back in 2009 and the responses go from the serious to the idiotic to the hilarious real quick. XD

This should be community wiki, for its entertainment value as well as for its being a cautionary tale on the dangers of having a head made of Legos.

Pretty much sums it up.  XD

It gave me the laugh I needed in what was an otherwise boring morning. (^_^)y

 

 

About Imnotgoing Sideways

I'm a Second Life avatar with my own cookie jar! (^_^)y
This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

One Response to Asking The Stack XD

  1. Ferd says:

    I like the answer x = -5 or something like that, as the problem didn’t specify which negative number.

Leave a Reply

Your email address will not be published. Required fields are marked *