MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

two's complement function


Can anyone think of a better way to produce a list of digits of a two's
complement integer than the following?

tc[n_, bits_:32] :=
If[Positive[n], IntegerDigits[n, 2, bits],
IntegerDigits[FromDigits[IntegerDigits[n, 2, bits] /. {1 -> 0, 0 -> 1}, 2] +
1, 2, bits]]

Thanks,
Ken



  • Prev by Date: Negation of Negation wanted
  • Next by Date: Re: M5.0 keyboard shortcuts with linux
  • Previous by thread: Re: Negation of Negation wanted
  • Next by thread: Re: two's complement function