C language & | ~ > [closed]

This is fundamental and so there are many potential applications, but here’s a specific industrial example:

Suppose you’re sending a bunch of command and/or status info between devices. To avoid wasting bandwidth (particularly if you’re using a slower type of connection such an old 9-pin serial connection, which are still used on industrial devices), you very well may “pack” and “unpack” the data. In the case of two-state signals, this means that each byte can hold up to eight independent statuses. To get the status of bit 6, you could do something like this:

status = (dataByte & 0x40) >> 6;

In the above line, 0x40 is a bitmask that results in all bits being zero except for bit 6. The shift right by 6 converts the resulting value–0x40 or 0x00–to 1 or 0.

Take a look at this brief section for standard, related examples: Bit Manipulation in C

Leave a Comment