Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!amdahl!ames!ucbcad!ucbvax!decvax!decwrl!spar!hunt From: hunt@spar.SPAR.SLB.COM (Neil Hunt) Newsgroups: comp.lang.c Subject: Re: Printing binary (was: Re: Why college?) Message-ID: <1668@spar.SPAR.SLB.COM> Date: Mon, 26-Oct-87 12:36:55 EST Article-I.D.: spar.1668 Posted: Mon Oct 26 12:36:55 1987 Date-Received: Thu, 29-Oct-87 00:00:13 EST References: <35@ateng.UUCP> <3194@sol.ARPA> <2783@xanth.UUCP> Reply-To: hunt@spar.UUCP (Neil Hunt) Organization: Schlumberger Palo Alto Research - CASLAB Lines: 28 In article <2034@gryphon.CTS.COM> greg@gryphon.CTS.COM (Greg Laskin) writes: >In article <1646@spar.SPAR.SLB.COM> hunt@spar.UUCP (Neil Hunt) writes: >>This is crying out for recursion: >> void >> print_binary(value, bits) >> int value, bits; >> { >> if(--bits > 0) >> print_binary(value >> 1, bits); >> putchar((value & 0x1) + '0'); >> } >> >Why would this cry out to incur the additional overhead of recursion >just to flip the order of the bits around when you could easily pick >the bits off the left side and use a simple loop to do the job? I knew that someone would ask this. Check out all the stuff that has to happen to get the binary bits displayed on the screen, or written to a file, and see if building a new stack frame 31 times is a significant penalty. One of the basic rules about writing code is only to sacrifice clarity for efficiency when it will make a difference ! > >Pretty algorithm though. Thankyou ! Neil/.