Click here to Skip to main content
15,887,477 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: zero int? Pin
KP Lee3-Dec-11 10:21
KP Lee3-Dec-11 10:21 
GeneralRe: zero int? Pin
harold aptroot3-Dec-11 10:42
harold aptroot3-Dec-11 10:42 
GeneralRe: zero int? Pin
KP Lee3-Dec-11 11:06
KP Lee3-Dec-11 11:06 
GeneralRe: zero int? Pin
harold aptroot3-Dec-11 11:19
harold aptroot3-Dec-11 11:19 
GeneralRe: zero int? Pin
Chris Berger22-Nov-11 8:46
Chris Berger22-Nov-11 8:46 
GeneralRe: zero int? Pin
greldak23-Nov-11 2:59
greldak23-Nov-11 2:59 
GeneralRe: zero int? Pin
CDP180223-Nov-11 3:03
CDP180223-Nov-11 3:03 
GeneralRe: zero int? Pin
KP Lee3-Dec-11 0:40
KP Lee3-Dec-11 0:40 
That would apply in a 1's comp environment on it's integer type. Certainly not on a Windows OS which is 2's comp like every other system that realized 2's comp is a superior mathematical process.

On 1's comp with a 32 bit integer, -0 is for every bit set to 1. In 2's comp every bit as 1 in a signed integer is always -1

On 1's comp, this is how you get -0:
x=-1
x=x+1

When you print it, you get "0", not "-0", but internally it's still -0.

When you add 1 to -0, it first converts all the bits from 1 to 0 and then because you are changing signs, you add an additional 1 to the number so it becomes 1. -1 + 10 would produce 8 and then add 1 to get 9. You've got that extra step of adding or subtracting 1 to be done every time a mathematical operation changes case in either direction with the one exception of reaching -0.

2's comp uses no additional steps when changing cases
In SQL:
select (-2*1024)*1024*1024
select (2*1024)*1024*1024
will produce -2147483648 in the first result, the second will get the following error:
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type int.
Take out the parens and both fail with the same error.
In C# with type int, both will produce -2147483648 (Assuming checked isn't applied.)

modified 3-Dec-11 6:48am.

GeneralRe: zero int? Pin
harold aptroot23-Nov-11 4:12
harold aptroot23-Nov-11 4:12 
GeneralRe: zero int? Pin
jsc4223-Nov-11 7:44
professionaljsc4223-Nov-11 7:44 
JokeRe: zero int? Pin
harold aptroot23-Nov-11 8:01
harold aptroot23-Nov-11 8:01 
JokeRe: zero int? Pin
Stefan_Lang12-Dec-11 6:18
Stefan_Lang12-Dec-11 6:18 
GeneralRe: zero int? Pin
BobJanova23-Nov-11 5:14
BobJanova23-Nov-11 5:14 
GeneralRe: zero int? Pin
CDP180223-Nov-11 5:17
CDP180223-Nov-11 5:17 
GeneralRe: zero int? Pin
AspDotNetDev23-Nov-11 6:45
protectorAspDotNetDev23-Nov-11 6:45 
GeneralRe: zero int? Pin
harold aptroot23-Nov-11 7:24
harold aptroot23-Nov-11 7:24 
GeneralRe: zero int? Pin
AspDotNetDev23-Nov-11 11:08
protectorAspDotNetDev23-Nov-11 11:08 
GeneralRe: zero int? Pin
KP Lee3-Dec-11 1:51
KP Lee3-Dec-11 1:51 
GeneralRe: zero int? Pin
harold aptroot3-Dec-11 2:14
harold aptroot3-Dec-11 2:14 
AnswerRe: zero int? Pin
Nagy Vilmos21-Nov-11 23:52
professionalNagy Vilmos21-Nov-11 23:52 
GeneralRe: zero int? Pin
CDP180221-Nov-11 23:57
CDP180221-Nov-11 23:57 
GeneralRe: zero int? Pin
Nagy Vilmos21-Nov-11 23:58
professionalNagy Vilmos21-Nov-11 23:58 
GeneralRe: zero int? Pin
CDP180222-Nov-11 0:08
CDP180222-Nov-11 0:08 
GeneralRe: zero int? Pin
PIEBALDconsult22-Nov-11 1:51
mvePIEBALDconsult22-Nov-11 1:51 
GeneralRe: zero int? Pin
CDP180222-Nov-11 2:24
CDP180222-Nov-11 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.