I'm working through K&R and getting stuck on the bitwise operation part...

I'm working through K&R and getting stuck on the bitwise operation part. I'm trying to start with the invert function first since it seems easiest, and I'm not even trying to do the number of bits aspect of it yet. For some reason, when I put in my input of 215(11010111), I'm getting out -217, when I'm expecting to get 43 (00100111). Does anyone know what the issue is? I thought it might be because my input was a standard int before, but even changing it to unsigned doesn't seem to help.

Attached: bitShifting.png (666x717, 67.79K)

Why are you expecting to get 43? You only invert n bits starting from p.

If you just want to invert without positions, it's literally just ~x.

To clarify, my intention is to copy the bits in x to the right of the place starting at p into temp, then shift x to the right to remove those bits, take the 1's compliment of the remaining bits in x, shift it back to the left so the right bits are 0, and then place the bits from temp back into those places. Is this a valid approach?

Sorry if it wasn't clear. I'm not doing the n bits part yet, I just wanted to check if my implementation of flipping from a specific place worked, which I don't think it does. In this case all 4 bits to the right of p in x should be reversed, which should change 215 into 43 if I'm not mistaken

why use vim as a code editor?

I don't feel the need to use an IDE just to do basic C exercises

>funciton
user you don't like functions?

Excuse my retardation

the inverse of 215 starting at position 4 should be, for unsigned ints:
215 (11010111)
200 (11001000)
The least significant bit (the right-most bit) is position '0'.

Sorry I don't think I get what you're saying. The invert should be conducted towards the LSB, rather than away from it?

Yeah. Check page 49, where this exercise is located, and observe their implementation of the getbits function. That's how it works, and presumably they want invert to work similarly given it takes the same parameters.

i would suggest writing some "print_bin()" function first. and then do your thing one step at a time and call print_bin() to get a binary view of what's going on.

Oh yeah that makes sense, thanks.
This helped immensely. I realize now that my usage of or is causing the sign bit to get flipped because of how I have it written.
Thanks everyone

nice thread

btw, i would also suggest not using signed types for bitwise stuff. alot of bitwise operations on signed type is undefined.

>main()
don't use functions with unspecified arguments, they're deprecated in c99 and generally not a good thing to use since no type checking can be performed on such function. use main(void) instead.

>non static functions
you should get into the habit of declaring all functions as well as all global variables as static. unless you're trying to use them in another file (translation unit), then you should declare them in a header file.

This is the fault of meme falling idiots on this board. If you want to learn C, don't get K&R ffs, get a modern textbook.

I feel that it's easier to learn bitwise math in assembly than it is in C. There's less syntax in the way.

I appreciate the tips, will use them going forward
It may be a meme but I like it enough. I can pick up new conventions if I need but I recognize the concern with just learning the modern standard outright
Maybe I'll check it out after since I've liked the little bit of assembly I've done. In any case I got what I wanted done and understand the logic behind it much better which was my overall objective

>get a modern textbook.
all of which are utter dogshit. it's much easier and better to learn through K&R and then pick up the "modern idioms" by reading good quality open source code; such as the linux kernel, OpenBSD, suckless softwares etc.

>2022
>The absolute state of Any Forums