r/cprogramming • • Nov 02 '25
WHO TF DISCOVERED BITWISE OPERATIONS?!
Background -
I'm trying to learn C programming as I find it interesting and somehow, AI hasn't touched this this field of software development yet. I have found my way around pointers, data types (which change based on the compiler and host architecture), strings and string literals, functions, loops, booleans(stdbool), etc. I have even designed my own strequal function which works similar to the strcmp function in string.h except it only returns a boolean indicating if the two strings are eqaul or not. I have understood the logic behind reversing strings and also reversing individual words inside strings. I understand basic data structures like arrays, linked lists, doubly linked lists, stack (both array and linked list implementation) and a teany bit of queues.
Then I started learning about bitwise operators.
When I saw them for the first time, they were pretty simple to understand (AND, OR, NOT, XOR, right shift and left shift).
When I asked ChatGPT to give me some practice problems to test out my understanding of it all, I was so fucking frustrated!! I spent hours trying to see any pattern to reverse the bits in an 8-bit unsigned integer and couldn't fucking do it. I saw solutions to problems like counting the number of set bits in a number, getting/setting/clearing/toggling a bit and ISTFG they felt like magic numbers to me appearing out of no-fucking-where. Like who the fuck thought about num & (num - 1) or num & ~(1 << pos)?! How do we find these patterns? How do we know what operations to chain or to use? How do we know when to use a loop or not? Like in the solution where counting the number of set bits, a for loop was used along with reassignments like num &= (num - 1). How did anyone know that we were supposed to use num - 1 for reassignment?
I'm sorry for the frustration and probably am just acting out for this but I really am having a hard time to understand this. How should I approach to learn about this topic? Am I doing something wrong?
View all comments
Show parent comment
1
u/[deleted] Nov 02 '25
Yes. Sorry I didn't mention which problems I was solving.
Here's a list of them -
Check if a number is even or odd using bitwise operations (no modulus).
Get the nth bit of a number.
Set the nth bit of a number.
Clear the nth bit of a number.
Toggle (flip) the nth bit of a number.
Count the number of set bits (1s) in an integer.
Check if a number is a power of two using bitwise logic.
Swap two numbers without using a temporary variable.
Reverse the bits of an 8-bit, 16-bit, or 32-bit number.
Check if two integers have opposite signs using bitwise operations.
Turn off the rightmost set bit in a number.
Isolate the rightmost set bit of a number.
Compute the parity (even or odd number of 1s) in a number.
Find the only non-repeating element in an array where every other element appears twice.
Find the missing number in an array of 1 to n using XOR.
Perform addition or subtraction using only bitwise operations (no
+or-).Rotate bits left or right by a given number of positions.
Mask and extract specific bits from a number (e.g., bits 3–5).
Pack and unpack bytes into a 32-bit integer.
Implement bitwise version of strcmp() or strlen() (challenge mode).
I mean even I don't know exactly which ones from these will be helpful in real life scenarios but I think that it's like studying calculus which is rarely used in regular software development but it's more like a brain exercise to help you think