Bit Manipulation in The C Programming Language
C has direct support for bitwise operations that can be used for bit manipulation. In the following examples, n is the index of the bit to be manipulated within the variable bit_fld, which is an unsigned char being used as a bit field. Bit indexing begins at 0, not 1. Bit 0 is the least significant bit.
- Set a bit
- Clear a bit
- Toggle a bit
- Test a bit
When using an array of bytes to represent set of bits, i.e., a bit array or bitset, the index of the byte in the array associated with a bit n can be calculated using division:
where n is the index of the given bit and CHAR_BIT gives the number of bits in a C char.
The index of the bit within the byte indexed by the above can be calculated via a modulo operation:
n % CHAR_BIT(Note: unsigned char is typically used in C to represent a byte and CHAR_BIT is most often 8 on modern processors. % is the C modulo operator.)
Read more about this topic: Bit Manipulation
Famous quotes containing the words bit, manipulation, programming and/or language:
“A slight digression: that bit about my mother was a deliberate lie. In reality, she was a woman of the people, simple and coarse, sordidly dressed in a kind of blouse hanging loose at the waist. I could, of course, have crossed it out, but I purposely leave it there as a sample of one of my essential traits: my light-hearted, inspired lying.”
—Vladimir Nabokov (18991977)
“The principle that human nature, in its psychological aspects, is nothing more than a product of history and given social relations removes all barriers to coercion and manipulation by the powerful.”
—Noam Chomsky (b. 1928)
“If there is a price to pay for the privilege of spending the early years of child rearing in the drivers seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.”
—Melinda M. Marshall (20th century)
“All official institutions of language are repeating machines: school, sports, advertising, popular songs, news, all continually repeat the same structure, the same meaning, often the same words: the stereotype is a political fact, the major figure of ideology.”
—Roland Barthes (19151980)