Bit manipulation: Difference between revisions

Content deleted Content added
Bit manipulation operations: add shift reverse etc. mention
Bit manipulation operations: add ref of binary and ternary logical operations
Tags: Mobile edit Mobile web edit Advanced mobile edit
Line 45:
{{see also|Bit Manipulation Instruction Sets}}
 
Processors typically provide only a subset of the useful bit operators. Programming languages don't directly support most bit operations, so idioms must be used to code them. The 'C' programming language, for example provides only bit-wise AND(&), OR(|), XOR(^) and NOT(~). Fortran provides AND(.and.), OR (.or.), XOR (.neqv.) and EQV(.eqv.). When the full set of [[Bitwise_operation#Bitwise_operators|bitwise operators]] are provided they are [[boolean functions]] of either two or three operands.

Algol provides syntactic bitfield extract and insert. When languages provide bit operations that don't directly map to hardware instructions, compilers must synthesize the operation from available operators.
 
An especially useful bit operation is ''count leading zeros'' used to find the high set bit of a machine word, though it may have different names on various architectures.<ref>On most Intel chips, it's BSR (bitscan reverse), though newer SoCs also have LZCNT (count leading zeros)</ref> There's no simple programming language idiom, so it must be provided by a compiler intrinsic or system library routine. Without that operator, it is ''very'' expensive (see [[Find first set#CLZ]]) to do any operations with regard to the high bit of a word, due to the asymmetric carry-propagate of arithmetic operations. Fortunately, most cpu architectures have provided that since the middle 1980s. An accompanying operation ''count ones'', also called [[Popcount]], which counts the number of set bits in a machine word, is also usually provided as a hardware operator.