Path: utzoo!attcan!uunet!mcsun!hp4nl!phigate!ehviea!leo From: leo@ehviea.ine.philips.nl (Leo de Wit) Newsgroups: comp.lang.c Subject: Re: setting tab stops Message-ID: <834@ehviea.ine.philips.nl> Date: 18 Jul 90 16:35:12 GMT References: <1037@rossignol.Princeton.EDU> Reply-To: leo@ehviea.UUCP (Leo de Wit) Organization: Philips I&E Eindhoven Lines: 20 In article <1037@rossignol.Princeton.EDU> tr@samadams.princeton.edu (Tom Reingold) writes: |This question is not relevant to C, but I saw a nice algorithm, and I |think I saw it in comp.lang.c. Someone posted a very terse way of |calculating how many spaces to move given an input of tabs. I think it |used the modulo operator and some bitwise operators. It was far too |terse and elegant for me to think of. Can someone express what I am |thinking of? The assumption is that the terminal tabs at every eighth |position. |-- If I understand you correctly, you want to know how many spaces to output given an input of tabs (let's call it ntabs) and a current column position (let's call it ccpos), so that the resulting column position would be the same? The current position is ccpos % 8 columns past the last tabstop, so you must move - (ccpos % 8) columns to get back to the previous tabstop, then 8 columns forward for each tab. This results in 8 * ntabs - (ccpos % 8) spaces, which also could be expressed as (ntabs << 3) - (ccpos & 7). Leo.