MathGroup Archive 2003

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

Search the Archive

two's complement function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44035] two's complement function
  • From: "Ken Morgan" <kemorgan at vt.edu>
  • Date: Sat, 18 Oct 2003 03:12:38 -0400 (EDT)
  • Organization: Virginia Tech, Blacksburg, Virginia, USA
  • Sender: owner-wri-mathgroup at wolfram.com

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