Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Twos-complement/Nim/twos-complement.nim
Normal file
11
Task/Twos-complement/Nim/twos-complement.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std/[strformat, strutils]
|
||||
|
||||
func twosComplement[T: SomeSignedInt](n: T): T =
|
||||
## Compute the two's complement of "n".
|
||||
not n + 1
|
||||
|
||||
echo &"""{"n":^15}{"2's complement":^15}{"-n":^15}"""
|
||||
for n in [0i32, 1i32, -1i32]:
|
||||
echo &"{n.toHex:^15}{twosComplement(n).toHex:^15}{(-n).toHex:^15}"
|
||||
for n in [-50i8, 50i8]:
|
||||
echo &"{n.toHex:^15}{twosComplement(n).toHex:^15}{(-n).toHex:^15}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue