Why would we want to change bit strings with bitwise operators?

Question

Why would we want to change bit strings with bitwise operators?

Answer

The use cases for bit strings are far and wide, but they’re super common in networking problems. Without going into any particular example, a general use of bit strings, including flipping each bit, is to check the output of a bit string compared with another bit string to perform some action for each bit.
For example, if we compare two bit strings and see that the result is 0101 0000, that might tell your program to perform a particular action. It’s a very powerful tool to have because with just those 8 bits, we can have a total of 2^8 possible operation codes!

2 Likes

That “answer” is about as useful as teats on a boar hog. :face_with_raised_eyebrow:

3 Likes

The Answer is a broad an answer as you would need. My background is networking. Subnets and supernets are calculated using bits and hex depending on ipv4 or ipv6. With this you can easily build yourself a subnet calculator for easy subnets if you do not want to have to do that by hand or head. we had to do this all on paper to learn the processing. Additionally if you wanted to do a rule matching where your IPv4 address is in bits so you do not need to convert it to base 10 you can do rule matching faster. Like the answer suggests not everyone will need this skill.

3 Likes

A lot of mathematical operations will be much faster using bitwise operators.
In general, to multiply a number^(2N), simply shift the bits to the left by N positions. To divide a number by (2N), shift the bits to the right by N positions. This is faster than using mathematical operators if speed performance and code size is critical.

2 Likes

Thank you for these answers! These are always helpful for beginners like me.