Re: two's complement function
- To: mathgroup at smc.vnet.net
- Subject: [mg44124] Re: two's complement function
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Thu, 23 Oct 2003 07:15:41 -0400 (EDT)
- Organization: The University of Western Australia
- References: <bmqqei$rk4$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <bmqqei$rk4$1 at smc.vnet.net>, "Ken Morgan" <kemorgan at vt.edu> wrote: > 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]] Why not use bitwise operations? If you execute the following cell FrontEndExecute[FrontEnd`HelpBrowserLookup[ "MainBookLink","Two's complement representation"]] it will direct you to appropriate section of the online documentation. Cheers, Paul -- Paul Abbott Phone: +61 8 9380 2734 School of Physics, M013 Fax: +61 8 9380 1014 The University of Western Australia (CRICOS Provider No 00126G) 35 Stirling Highway Crawley WA 6009 mailto:paul at physics.uwa.edu.au AUSTRALIA http://physics.uwa.edu.au/~paul