1

How do I use the Win10 calculator in Programmer mode to calculate the 2's complement value of the input binary representation. The calculator seems to treat the input binary sequence always an unsigned sequence. I was wondering if there is a way to make it treat the sequence as a 2's complement sequence instead.

Edit: As per the below comment by Peter, clarifying my question: I am looking to calculate the 2's complement value of a binary sequence. Sorry for the confusion.

phuclv
  • 30,396
  • 15
  • 136
  • 260
rgbk21
  • 13

1 Answers1

0

There are simply no unsigned types in Windows calculator. If you turn on the top bit of the value then it's always signed. You can turn on the "Big toggling keypad" by clicking the button above the "Bit Shift" and then click on the top bit to see

Since 2's complement just mean the negated value, you simply press the +/- button on the left of the zero key and the 2's complement value of the current value will be taken

For example if you enter 2 and press the button then it'll become -2 in decimal, and a binary value with all ones except in the least significant bit. Press +/- again and the 2's complement of -2 will be displayed

Another thing to note is that the middle button determines how many bits you want to work with. If you're working with DWORD (i.e. 32-bit) then entering a 16-bit hex or binary value will always return a positive value, but if you change to WORD (16-bit) then the value will become negative if the 16th bit is 1

Calculator negative value example


Slightly off-topic, but I suggest using PowerShell for more complex programming-related calculations, especially unsigned 64-bit operations because it supports any types in .NET framework. The Windows calculator currently doesn't support unsigned or decimal types, thus doing 64-bit unsigned operations will be tricky. Many operators in PowerShell are a little bit longer than the counterpart in C# but you'll get the result faster than clicking on the calculator buttons. It's not easy to know all the shortcut keys in the calculator app so you'll have to use the mouse at some point

To get the two's complement value of x just use -x. Or you can get the result from the definition of two's complement:

$x = 123
$complement_x = ($x::MaxValue + 1) - $x

PowerShell is like a front-end to the .NET framework, so any .NET or Win32 functions can be called from PowerShell. The math functions above are mainly from the .NET Math class and Numerics namespace. You'll put class .NET types inside [], like [math] or [system.math] for the Math class (PowerShell is case-insensitive). See Doing Math with System.Math. Here are some other things that may be useful to programmers:

  • Bitwise operations (bitwise operators begin with -b)

    [uint64]::MaxValue/3 + (-bnot 20) + (1L -shl 22) + (0x23 -band 0x34)
    
  • Big integer math: [bigint]::Pow([uint64]::MaxValue, 20)

  • Arbitrary integer and floating-point math expressions

    1.56 + 0.23/[math]::Pow([math]::Sqrt([math]::Log(20) + [math]::Sin([math]::PI/3)), 4)
    
  • Math on decimal type (128-bit): 1.23d * 3.45d / 28

  • Math on 128-bit integer types

  • Calculate file or object sizes: Use number suffixes 12.5GB + 5.8MB + 1392KB for binary units and 12.5e9 + 5.8e6 + 1392e3 for decimal units (G = 1e9, M = 1e6, K = 1e3)

  • Convert to/from base64: [Convert]::ToBase64String and [Convert]::FromBase64String

  • Date/time manipulation. For example convert from raw Epoch values to datetime and vice versa

    [datetime]::FromFileTime(0x01d15614cbaee92c)
    [datetime]::ParseExact("08-12-2012","dd-MM-yyyy", `
            [System.Globalization.CultureInfo]::InvariantCulture)
    
  • String formatting and base conversion. Anything that String.Format in .NET supports will work. For more information read about the formatting operator. You can also do advanced string and regex manipulation. Some examples:

    'somestring'.Substring(4) * 3 -replace 'ings', 'eet'
    '{0:X}' -f (0x12 + 34)
    [convert]::ToString(0x12 + 34, 16)
    'This is an emoji' + [char]::ConvertFromUtf32(0x1F60A)
    
  • Direct XML, CSV, JSON... manipulation

  • Call functions in *.DLL/*.SO files directly

  • GUI programming. Yes PowerShell is powerful enough and many people do write WinForm apps in PowerShell. Here's a small sample clipboard history app

    Clipboard history app in PowerShell

Example for the things above

PowerShell sample

For more information you can read

or follow Dr Scripto's blog

phuclv
  • 30,396
  • 15
  • 136
  • 260