Fog Creek Software
Discussion Board

Quirk with Windows calculator

I travel about 3 hours each way, to and then fro, everyday for work. Last night, I reached home at 1 AM. My dad was sleeping in my room, so I had to take his room. He had his books under the matress and I couldn't figure out why I couldn't sleep on the matress. So I quickly sat up on bed. I wasn't all that sleepy too. I had this thing itching me that I had to know the binary to decimal to hex conversions better. I always get these fits every few months, when I feel like. So I spent the entire night till 6 AM doing this stuff on a scribble pad. What a night it was. I have many questions and a few things I observed from last night. Just one of them is here.

The binary representation of the decimal numeral 3430 is 1101 0110 0110 or &HD66&. If this were an integer/word, then it would be padded with another quadruplet of high order bits and thus would look like:

0000 1101 0110 0110

Using two's complement, the negative of this number would be derived thusly:

3430                      =                    0000 1101 0110 0110
Complement          =                    1111 0010 1001 1001
Add 1                      =                    +                              1
                                                    1111 0010 1001 1010
or                                                  F        2      9      A

Therefore, &HF29A& = -3430 then.

I couldn't wait for the morning, when I'd come to the office today morning to check this out in the Windows calculator.

I typed a decimal value of +3430 and looked at its binary to see if it was 1101 0110 0110. I then took its hexadecimal representation and it was D66, like I expected.

Then, I typed a decimal value of -3430 and converted to binary. The value tallied with my calculation. The binary was indeed 1111 0010 1001 1010, and the hex was F29A.

Ok, nothing new here. Why am I making this post? Because then, I tried to do the reverse. I typed in the binary value of -3430 into the calculator to see if it would do a two's complement, and give me -3430 as the anwer.


1111 0010 1001 1010

into the calculator gave me the one's complement of this binary i.e. 62106, and the hex was yet F29A for the Word.

While this would be true if it were an unsigned Integer/Word, it does not seem right for signed integers.

Conclusion: The Windows Calculator does not have a mechanism to convert binary to decimal for negative/signed numerals, i.e it will always assume you are writing an unsigned numeral.

New for me. I thought I'll share it with you folks.

Sathyaish Chakravarthy
Friday, April 16, 2004

It's because there's no way for it to know if you're typing (for example) 16 bits of a 32 bit value (bit 31 the sign) or 16 bits of a 16 bit number (bit 15 the sign).

Friday, April 16, 2004

Exactly. You see while converting decimal to binary, it does not pad the high order bits. The high order bits are implicit.

It could have had a command button or some radio button for doing the reverse too. Why must the user unnecessarily specify the size/range of the data? Why can't the high order zeros become implicit this time? Any case, there's are radio buttons for the data-size.

Sathyaish Chakravarthy
Friday, April 16, 2004

What could be done is to incorporate by which a user can tell the calculator, "Hey, negative number inside. Handle with care." A radio button, may be.

Sathyaish Chakravarthy
Friday, April 16, 2004

>incorporate by which

I typed "incorporate a way by which" and it ate the words in the middle. Gives me a feeling this is not the first time this is happening. And I've been cursing myself sometimes when it wasn't my fault. I can't be sure if I typed it wrong.

Sathyaish Chakravarthy
Friday, April 16, 2004

*  Recent Topics

*  Fog Creek Home