Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: PUZZLES AND PROBLEMS Message-ID: <1384.UUL1.3#5129@willett.UUCP> Date: 25 Jul 90 03:01:30 GMT Organization: String, Scotch tape, and Paperclips. (in Pgh, PA) Lines: 33 Category 3, Topic 35 Message 127 Mon Jul 23, 1990 W.BADEN1 [Wil] at 21:25 PDT Roger Bicknell writes, rb> No No No, he said - you definitely require a Loop! I said - Na - Strictly speaking you win because he said "definitely" and any loop with a constant number of iterations can be unrolled. Here is a 16-bit version. : lg ( i -- log2 i) 2/ DUP IF EXIT THEN 2/ DUP IF DROP 1 EXIT THEN 2/ DUP IF DROP 2 EXIT THEN ... and so on ... DROP 15 ; If there's any money riding on this, you collect. But I'm sure the three of us agree that this isn't what he meant. The problem you're thinking of is to test for a number's being a power of 2: : powerOf2? ( n -- flag) DUP 1- AND 0= ; Or if you don't want 0 to be a power of two: : powerOf2? ( n -- flag) DUP IF DUP 1- AND 0= ( flag) THEN ; Procedamus in pace. ----- This message came from GEnie via willett through a semi-automated process. Report problems to: uunet!willett!dwp or willett!dwp@hobbes.cert.sei.cmu.edu