From: Iban
Subject: math operation and bits operations
Date: 
Message-ID: <3BAA6460.47BA4B04@yahoo.fr>
Greetings,

I want your advise for a little function that I need.
I have to "reconstruct" a bit mask according to the following rules (its
probably very standard) :
    is given : - a bit mask of member to change.
                    - a bit mask of new value
                    - the original bit mask

example :
    000100110 original bit mask
    100110011 bit mask of member to change
    101110111 bit mask of new value
    --------
    100110111 bit mask result

I know that there is a curious bit (number 6) in new value but it
doesn't matter (it is for the example).

Here is my solution. I'm not totally satisfy of it. But I have some
logical problems with this function. Is there any way to do that with a
complicated but more "logical", combination of and, or, nand, xor, ....
? And would it be more efficient ?

(defun compute-mask (mask mask-of-member-to-change new-value)
  (loop with result-mask = 0
            for i from 0 to 9
            if (logbitp i mask-of-member-to-change)
            do (when (logbitp i new-value) (incf result-mask (ash 1 i)))

            else do (when (logbitp i mask) (incf result-mask (ash 1 i)))

            finally (return result-mask)))

Thanks.

From: ···@itasoftware.com
Subject: Re: math operation and bits operations
Date: 
Message-ID: <8zf8bn3b.fsf@itasoftware.com>
Iban <········@yahoo.fr> writes:

> Greetings,
> 
> I want your advise for a little function that I need.
> I have to "reconstruct" a bit mask according to the following rules (its
> probably very standard) :
>     is given : - a bit mask of member to change.
>                     - a bit mask of new value
>                     - the original bit mask
> 
> example :
>     000100110 original bit mask
>     100110011 bit mask of member to change
>     101110111 bit mask of new value
>     --------
>     100110111 bit mask result
> 
> I know that there is a curious bit (number 6) in new value but it
> doesn't matter (it is for the example).
> 
> Here is my solution. I'm not totally satisfy of it. But I have some
> logical problems with this function. Is there any way to do that with a
> complicated but more "logical", combination of and, or, nand, xor, ....
> ? And would it be more efficient ?
> 
> (defun compute-mask (mask mask-of-member-to-change new-value)
>   (loop with result-mask = 0
>             for i from 0 to 9
>             if (logbitp i mask-of-member-to-change)
>             do (when (logbitp i new-value) (incf result-mask (ash 1 i)))
> 
>             else do (when (logbitp i mask) (incf result-mask (ash 1 i)))
> 
>             finally (return result-mask)))

Here's my solution

(defun compute-mask (mask mask-of-member-to-change new-value)
    (logxor mask 
            (logand mask-of-member-to-change
                    (logxor mask new-value))))

The bits won't change if they are the same between mask and new value,
so the innermost logxor finds the bits to flip.  The logand removes
those bits that ought not change, and the outer logxor flips the
remaining ones.

00001111  original
00110011  bits to modify
01010101  new value
--------
00011101  expected result


(format t "~8,'0b" (compute-mask #b00001111 #b00110011 #b01010101))
=> 00011101
From: Ilya
Subject: Re: math operation and bits operations
Date: 
Message-ID: <9oi6ha$r3v$1@news.inter.net.il>
···@itasoftware.com wrote:

> Iban <········@yahoo.fr> writes:
> 
>> Greetings,
>> 
>> I want your advise for a little function that I need.
>> I have to "reconstruct" a bit mask according to the following rules (its
>> probably very standard) :
>>     is given : - a bit mask of member to change.
>>                     - a bit mask of new value
>>                     - the original bit mask
>> 
>> example :
>>     000100110 original bit mask
>>     100110011 bit mask of member to change
>>     101110111 bit mask of new value
>>     --------
>>     100110111 bit mask result
>> 
>> I know that there is a curious bit (number 6) in new value but it
>> doesn't matter (it is for the example).
>> 
>> Here is my solution. I'm not totally satisfy of it. But I have some
>> logical problems with this function. Is there any way to do that with a
>> complicated but more "logical", combination of and, or, nand, xor, ....
>> ? And would it be more efficient ?
>> 
>> (defun compute-mask (mask mask-of-member-to-change new-value)
>>   (loop with result-mask = 0
>>             for i from 0 to 9
>>             if (logbitp i mask-of-member-to-change)
>>             do (when (logbitp i new-value) (incf result-mask (ash 1 i)))
>> 
>>             else do (when (logbitp i mask) (incf result-mask (ash 1 i)))
>> 
>>             finally (return result-mask)))
> 
> Here's my solution
> 
> (defun compute-mask (mask mask-of-member-to-change new-value)
>     (logxor mask
>             (logand mask-of-member-to-change
>                     (logxor mask new-value))))
> 
> The bits won't change if they are the same between mask and new value,
> so the innermost logxor finds the bits to flip.  The logand removes
> those bits that ought not change, and the outer logxor flips the
> remaining ones.
> 
> 00001111  original
> 00110011  bits to modify
> 01010101  new value
> --------
> 00011101  expected result
> 
> 
> (format t "~8,'0b" (compute-mask #b00001111 #b00110011 #b01010101))
> => 00011101
> 

Sorry i don't know lisp.
the logic is : 

(to_change and new_val) xor orig_val
From: Ilya
Subject: Re: math operation and bits operations
Date: 
Message-ID: <9oi6l9$r3v$2@news.inter.net.il>
···@itasoftware.com wrote:

> Iban <········@yahoo.fr> writes:
> 
>> Greetings,
>> 
>> I want your advise for a little function that I need.
>> I have to "reconstruct" a bit mask according to the following rules (its
>> probably very standard) :
>>     is given : - a bit mask of member to change.
>>                     - a bit mask of new value
>>                     - the original bit mask
>> 
>> example :
>>     000100110 original bit mask
>>     100110011 bit mask of member to change
>>     101110111 bit mask of new value
>>     --------
>>     100110111 bit mask result
>> 
>> I know that there is a curious bit (number 6) in new value but it
>> doesn't matter (it is for the example).
>> 
>> Here is my solution. I'm not totally satisfy of it. But I have some
>> logical problems with this function. Is there any way to do that with a
>> complicated but more "logical", combination of and, or, nand, xor, ....
>> ? And would it be more efficient ?
>> 
>> (defun compute-mask (mask mask-of-member-to-change new-value)
>>   (loop with result-mask = 0
>>             for i from 0 to 9
>>             if (logbitp i mask-of-member-to-change)
>>             do (when (logbitp i new-value) (incf result-mask (ash 1 i)))
>> 
>>             else do (when (logbitp i mask) (incf result-mask (ash 1 i)))
>> 
>>             finally (return result-mask)))
> 
> Here's my solution
> 
> (defun compute-mask (mask mask-of-member-to-change new-value)
>     (logxor mask
>             (logand mask-of-member-to-change
>                     (logxor mask new-value))))
> 
> The bits won't change if they are the same between mask and new value,
> so the innermost logxor finds the bits to flip.  The logand removes
> those bits that ought not change, and the outer logxor flips the
> remaining ones.
> 
> 00001111  original
> 00110011  bits to modify
> 01010101  new value
> --------
> 00011101  expected result
> 
> 
> (format t "~8,'0b" (compute-mask #b00001111 #b00110011 #b01010101))
> => 00011101
> 

sorry it was incorrect. i'll think about it