Subject: Re: How to write portable bitwise operation code? (or how not to think like a c hacker)
From: rpw3@rpw3.org (Rob Warnock)
Date: Sat, 07 Apr 2007 04:20:42 -0500
Newsgroups: comp.lang.lisp
Message-ID: <bvudnW_wetz3_4rbnZ2dnUVZ_oernZ2d@speakeasy.net>
A few hours ago, I wrote:
+---------------
|   (defun valid-netmask-p (x &optional (addr-width 32))
|     (and (plusp x)
|          (= (integer-length x) addr-width)
|          (let ((y (- (ash 1 addr-width) 1 x))) ; strip/cmpl 1st run of 1's
| 	     (= y (1- (ash 1 (integer-length y))))))) ; also a run of 1's?
+---------------

Upon further reflection, I noticed a *much* simpler way to do this
[and probably much faster, since it doesn't use INTEGER-LENGTH]:

    (defun valid-netmask-p (x &optional (addr-width 32))
      (= x (- (ash 1 addr-width) (logand x (- x)))))  


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607