# K & R C Programs Exercise 2-9.

K and R C, Solution to Exercise 2-9:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition)You can learn and solve K&R C Programs Exercise.
In C two complement number system, x &= (x-1) deletes the rightmost one bit in x.
We take (x-1) and add 1 to it to produce x. The rightmost 0-bit of x-1 changes to 1 in the result x. Therefore, the rightmost 1-bit of x has a corresponding 0-bit in x-1. This is why x & (x-1), in a two’s complement number system, will delete the rightmost 1-bit in x. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/int bitcount(unsigned x){	int b;	for(b = 0;x != 0;x &= x-1)		++b;	return b;}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 2-8.

K and R C, Solution to Exercise 2-8:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition) You can learn and solve K&R C Programs Exercise.
C Program that returns the value of the integer x rotated to the right by n bit positions. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/unsigned rightroot(unsigned x, int n){ int wordlength(void); int rbit;           //rightmost bit while(n-->0){  rbit=(x & 1)<<(wordlength()-1);  x=x>>1;  x=x|rbit; } return x; //word length computes the wor lengt of machine int wordlength() {  int i;  unsigned v = (unsigned~0;  for(i = 1;(v = v >> 1) > 0;i++)   ;  return i; } //main function to test the program,, you can try in different ways!#include <stdio.h> int main(void) {  unsigned x;  int n;  for(x = 0; x < 200; x += 25)   for(n = 1; n < 8; n++)    printf("%u, %d: %un", x, n, rightrot(x, n));  return 0; }`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.