How to Read a Bit in C++

Once you break out the tools, bit manipulation in C tin be a fascinating and useful thing. Setting a bit, covered in terminal week'south Lesson, is a big offset footstep. One time you've washed that, the functions to read and set a bit autumn into place naturally.

The goal in each function is to admission a specific scrap. From concluding week's code snippet, the post-obit statement is used:

          chip = ane << bit;        

Variable flake represents the flake position in a byte, give-and-take, or long word. Position 0 is at the far right. Position one comes next, and so on. The role sets bit 0 to the value 1. The left shift operator, <<, shifts that value left to the proper bit position.

To read a bit at a specific position, yous must mask out all other bits in the value. The operator that assists in that procedure is the bitwise & (and). Later on you mask out all the other bits, the value that remains is either zero or some other value. These ii atmospheric condition evaluate to FALSE and Truthful in the C language, which tells yous whether or not the scrap was set.

In Figure 1, yous encounter binary value 1 shifted twice to bit position 2, as in fleck = 1 << bit; where scrap equals ii.

Figure 1. Variable bit is set to binary position 2.

Figure 1. Variable bit is set up to binary position two.

One time the bit is positioned, an & (and) mask eliminates all other bits but position ii in the original value. The issue is either 0 or 4 depending on whether the bit was off or on, respectively. Figure ii illustrates how this operation works.

Figure 2. How an & (and) mask works.

Figure 2. How an & (and) mask works.

The function I wrote to read scrap condition is called bit_test(). It uses the same arguments as bit_set() from last week'southward Lesson:

int bit_test(char bit, char byte) {     bit = 1 << fleck;     render(bit & byte); }

The starting time statement sets the proper position for variable bit. The 2nd statement uses the & (and) mask to isolate that bit and render the value.

The value returned by bit_test() is either truthful, meaning the bit was set, or imitation when the flake is zilch.

The concluding flake manipulation office is bit_reset(), which resets a scrap's value to zero. This function uses the same arguments as bit_set() and bit_test(), which provides consistency.

To reset a flake, the bit_reset() function uses both the bitwise ^ (exclusive or) and & (and) operators. These operators assist to isolate the private fleck and set only that bit to 0.

Here is the role:

void bit_reset(char bit, char *byte) {     fleck = 1 << bit;     bit ^= 0xff;     *byte = *byte & bit; }

The statement bit ^= 0xff; inverts the bits. The cyberspace issue is to mask off all $.25 save for the one to reset, as illustrated in Effigy 3.

Figure 3. The ^ (xor) operator inverts the bit field, creating a mask.

Figure three. The ^ (xor) operator inverts the bit field, creating a mask.

Once the mask is set, the & (and) operator filters through equally but the one to reset. The end result is that only one chip is reset.

In next calendar week's Lesson, I put all 3 functions to work in a demo plan that manipulates bits in a char value.

chanterdoweed.blogspot.com

Source: https://c-for-dummies.com/blog/?p=1848#:~:text=To%20read%20a%20bit%20at,zero%20or%20some%20other%20value.

0 Response to "How to Read a Bit in C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel