This shows you the differences between two versions of the page.

cl:functions:logcount [2019/06/15 02:00] |
cl:functions:logcount [2019/08/19 17:00] (current) |
||
---|---|---|---|

Line 1: | Line 1: | ||

+ | ====== Function LOGCOUNT ====== | ||

+ | |||

+ | ====Syntax==== | ||

+ | * **logcount** //integer// → //number-of-on-bits// | ||

+ | |||

+ | ====Arguments and Values==== | ||

+ | * //integer// - an //[[CL:Glossary:integer]]//. | ||

+ | * //number-of-on-bits// - a non-negative //[[CL:Glossary:integer]]//. | ||

+ | |||

+ | ====Description==== | ||

+ | Computes and returns the number of bits in the two's-complement binary representation of //integer// that are "on" or "set". If //integer// is negative, the ''0'' bits are counted; otherwise, the ''1'' bits are counted. | ||

+ | |||

+ | ====Examples==== | ||

+ | <blockquote> | ||

+ | (logcount 0) <r>0 </r> | ||

+ | (logcount -1) <r>0 </r> | ||

+ | (logcount 7) <r>3 </r> | ||

+ | (logcount 13) <r>3 ; Two's-complement binary: ...0001101 </r> | ||

+ | (logcount -13) <r>2 ; Two's-complement binary: ...1110011 </r> | ||

+ | (logcount 30) <r>4 ; Two's-complement binary: ...0011110 </r> | ||

+ | (logcount -30) <r>4 ; Two's-complement binary: ...1100010</r> | ||

+ | (logcount ([[CL:Functions:expt]] 2 100)) <r>1 </r> | ||

+ | (logcount ([[CL:Functions:math-subtract|-]] ([[CL:Functions:expt]] 2 100))) <r>100 </r> | ||

+ | (logcount ([[CL:Functions:math-subtract|-]] ([[CL:Functions:math-one-plus|1+]] ([[CL:Functions:expt]] 2 100)))) <r>1 </r> | ||

+ | </blockquote> | ||

+ | |||

+ | ====Side Effects==== | ||

+ | None. | ||

+ | |||

+ | ====Affected By==== | ||

+ | None. | ||

+ | |||

+ | ====Exceptional Situations==== | ||

+ | Should signal **[[CL:Types:type-error]]** if its argument is not an //[[CL:Glossary:integer]]//. | ||

+ | |||

+ | ====See Also==== | ||

+ | None. | ||

+ | |||

+ | ====Notes==== | ||

+ | Even if the //[[CL:Glossary:implementation]]// does not represent //[[CL:Glossary:integers]]// internally in two's complement binary, **[[CL:Functions:logcount]]** behaves as if it did. | ||

+ | |||

+ | The following identity always holds: | ||

+ | |||

+ | <blockquote> | ||

+ | (logcount //x//) | ||

+ | ≡ (logcount ([[CL:Functions:math-subtract|-]] ([[CL:Functions:math-one-plus|1+]] //x//))) | ||

+ | ≡ (logcount ([[CL:Functions:lognot]] //x//)) | ||

+ | </blockquote> | ||

+ | |||