Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere can I find a bit shifting guide for C?
    text
    copied!<p>I have looked at <a href="https://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting">What are bitwise shift (bit-shift) operators and how do they work?</a> but I still find the concept of bit shifting difficult to understand.</p> <p>Can someone point me in the direction of a more basic guide to bit shifting in C. I expect it will be something really long since it will need to cover the whole subject.</p> <p>I really do not understand it but I want to learn it, so any help would be appreciated.</p> <p>I am learning the k&amp;r, and that is what this is for so I can do the exercises. I understand the basics, but I still cannot do correct bit shift operations.</p> <p><strong>EDIT here's the exersices from K&amp;R that have stumped me</strong></p> <p>Exercise 2-6: Write a function setbits(x, p, n, y) that returns x with the n bits thatbegin at position p set to the rightmost n bits of y, leaving the other bits unchanged.</p> <p>Exercise 2-7: Write a function invert(x, p, n) that returns x with the n bits that begin at poisiton p inverted (i.e., 1 changed into 0 and vice versa) leaving the others unchanged.</p> <p>Exercise 2-8: Write a function rightrot(x, n) that returns the value of the integer x rotated to the right by n bit positions</p> <p>Exercise 2-9: In a two's complement number system, x &amp;= (x-1) deletes the rightmost 1-bit in x. Explain why, Use this observation to write a faster version of bitcount</p> <p>These are exercises from the k&amp;R (the c programming language) book its the best c book there is but I am having trouble understanding bitshifting so I am having problems with these exercises.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload