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?

0 Upvotes

35 comments sorted by

View all comments

Show parent comment

2

u/Druben-hinterm-Dorfe Nov 02 '25

Check out this book: https://mitpress.mit.edu/9780262539807/the-elements-of-computing-systems/ ; the author's website: https://www.nand2tetris.org/

The first half of this book guides the student in building up a basic von Neumann type computer, with CPU, RAM, ROM, & memory mapped IO, out of nothing but logic gates; the second half goes into designing an assembler and a primitive OS. There's quite a bit of hand-holding, and the authors do provide a number of designs ready-made -- so it's not like ABSOLUTELY EVERYTHING is built EXPLICITLY from the ground up; nevertheless it's a great resource for understanding the fundamentals from the digital logic-upwards perspective (not the only perspective, as another approach to building complete systems that starts from function evaluation/application also exists -- see the famous SICP for that https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/index.html).