Using 2's compliment in gcalctool

Asked by Mike Godin on 2010-12-20

I would like to convert between negative decimal numbers and two's complement hex and binary (i.e. convert between decimal -1 and hex 0xFFFF in 16-bit mode). Apparently this is possible, as there was a bug report in 2007 dealing with the same issue (https://bugs.launchpad.net/ubuntu/+source/gcalctool/+bug/151676) that was labeled "Invalid" because the operation was possible... But it is still not clear how to do the operation.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gcalctool Edit question
Assignee:
No assignee Edit question
Solved by:
Mike Godin
Solved:
2010-12-20
Last query:
2010-12-20
Last reply:
2010-12-20
mycae (mycae) said : #1

cant you just compute 2^16-1 by hand?

mycae (mycae) said : #2

by "hand" i mean by typing that into the calculator :)

Mike Godin (godin) said : #3

Yes, that does indeed answer the question... but is there a quick way to enter 2^N in "programming mode? I suppose 2 < 15 would do it for 2^16, but what about 2^64 ?

There is a "2's" button that appears in programming mode, but it appears to be completely undocumented. One would expect that to do what I want. By plugging in random numbers, I think I've got it figured out...

X twos Y = X * (twos compliment of |Y|)
Note that if Y is negative, it must be > -[2^{base-12}])

So the answer to my question should probably be:
1 twos 1 = FFFF

Seems to work in 64 bit mode too.
1 twos 81985529216486896 = 1.836e19 = FEDCBA9876543210

Mike Godin (godin) said : #5

Thanks -- I'd missed that part of the manual. However, it seems to be incorrect, as the manual (in versions 5.28 and 5.29) states:

Function: Get the 2's complement
Button: 2's
Description: Calculates the 2's complement for the current value in the display area, using the word size configured in Section 3.6.2 ― To Set the Word Length.
Example: D723 2's
Result: FFFF28DD

Typing "D723 2's" results in a "Malformed Expression" error, at least with version 5.28.2.

Sam_ (and-sam) said : #6

Mmh, I'm not familiar with programming functions, but since gcalctool lately misses some functions anyway I found qalculate quite useful.

mycae (mycae) said : #7

>Example: D723 2's
I think this means type "d723" then press the button called "Twos"Example: D723 2's