Where can I find a guide to moving bits for C?

I looked at What are bit shift operators (bit-shift) and how do they work? but I still find the concept of bit shifting difficult to understand.

Can someone point me towards a simpler guide on bit offsets in C. I expect this to be something for a really long time, as this will have to cover the whole subject.

I really do not understand this, but I want to know it, so any help would be appreciated.

I am studying k & r, and that is exactly so, so I can do the exercises. I understand the basics, but I still can not perform the correct bit change operations.

EDIT here exersices from K&R that pushed me to

Exercise 2-6: Write a set of functions (x, p, n, y) that returns x with n bits located at position p set to the rightmost n bits of y, leaving the remaining bits unchanged.

Exercise 2-7: Write an invert function (x, p, n) that returns x with n bits that start with inverted poisiton p (i.e. 1 is changed to 0 and vice versa), leaving the rest unchanged.

Exercise 2-8: Write a function rightrot (x, n) that returns the value of an integer x rotated to the right by n bit positions

Exercise 2-9: On a system with two additional numbers, x & = (x-1) deletes the rightmost 1-bit in x. Explain why, use this observation to record a faster version of the bitcoin

k & R ( c), c, , .

+5
12

- - , , : .

, , , (, 6, 7, 3, 2) . , - C:

(7 >> 1)

, 7 1 .

, , , . , , , .

, , , . .

+7

, , .

, , . , . , , ​​ . //xor/inversion/etc. - . , , "". 1 0? 1 0?

Googling " ", , . , , ,

// 1234 in hex, you understand this is not 1234 in decimal? Do you understand
// how specifying in hex makes it easier to think about the binary representation?
unsigned short i = 0x1234; 
// flip all the bits in 0x1234
printf("%04x", ~i);

// Test - Is the most significant bit set?
// mask is a binary number with the most significant bit set. Do
// you understand why this is so?
unsigned short mask = 0x8000;   
if ((mask & i) == mask)
{
    printf("bit set!");
}
else 
{
    printf("bit cleared!");
}

// set the most significant bit
i = i | mask;
printf("Set the MSB of i, check it out: %i\n", i)

// set the second most significant bit
// Do you see how by shifting mask right one it becomes the next most significant bit?
i = i | (mask >> 1);

!

+4

. 10.

C- .

So

x = y << 3;

, - , , , "-"

x = z >> 2

.

K & R - , , C .

, , , ,

, 0xD, 1 , 0xE, lsbit 1101 , 1 1 1110

. , , 0xD 0 1101, 1 0110 a 0x6. 0 1011, 0xB .

- , ? , , 8- , , - bcde fghi, a - , - , e abcd fghi, i abcd efgh, , 8- 4- . ( , - 0 + 0 - , , ) i 0bcd efgh, , , 32- 64- .

, , abcd bcd0 shift abcd right two 00ab.

: , , , , , 2 , . 0x0D, , 0b00001101 < 2 = 0b00110100 0x34 0xD 13 0x34 52 . 52 13. 2 2. - , . , 0x34 2, 0xD, , , 4 0xFC, . C 0xFC → 1 0x7E, 0x7E +126 , -4/2 = 126? , . , , , , , , 0bQWER, , 0bQQwe, , . 0bQQQW .. , lsbit, 0bQWER , 0bWER0. -4 , 0xF8, -8, -4 -8, . , , , , asl, , lsl ( ), , . , , asl asr lsr, lsl.

-, , . 0x1234 , ?

0001001000110100  write out the bits 
x0001001000110100 shift right one
0000100100011010  because this is a rotate fill in the new bit with the bit that fell off on the prior operation

0000100100011010 
xx0000100100011010 
1000001001000110

C?

unsigned int rotate_right_one ( unsigned int x )
{
  unsigned int y;
  y = x & 1;  //save the bit that is about to fall off the right
  x >> = 1;  //logical rotate one bit
  x |= y<<31; //this assumes that unsigned int is 32 bits.
  return(x);
}

, .

, rotate, , . , 5 , ?

abcd
bcda  first rotate
cdab  second
dabc  third
abcd  fourth
bcda  fifth

?

abcd
bcda  one bit left.

5-4 = 1. asl, , , nbits-shift .

, , , , .

, :

for(count=0,r=1;r;r<<=1) if(r&some_variable) count++;

, C .

+1

2-6, 17, 4, 3, 15.
:

x = 17 10001

p = 4

n = 3

y = 15 01111

, ls y (111) x, 4. 11111 31. 3 x, , y, 3 , .

1st: n ~ 0 < n = 1111111111111000

2nd: n ~ (~ 0 < lt; n) = 0000000000000111

3rd: n 1 p n , p.    ~ (~ (~ 0 < n) < (p-n)) = 1111111111110001

4th: x n x p = 10001

( , , , , ,   , , ,   . , p 6, n ?)

y, n :

1st: ~ (~ 0 < n) n . 0000000000000111

2nd: y ~ (~ 0 < n) n y 0000000000000111

3rd: (y ~ (~ 0 < n)) < (p-n) n y p 0000000000001110

0000000000010001 = 17

0000000000001110 = 7 OR =

0000000000011111 = 31

:

return x ~ (~ (~ 0 < n) < (p-n)) | (y ~ (~ 0 < n)) < (-);

, . . ( , ). , , , , , , , , , , , , .

+1

, , . -?

, K & R, .

, , .

0

( ), Hackers Delight. , , . ( ) ...

0

.

2-6: setbits (x, p, n, y), x n , p n y, .

, 0 ( 0)

:

  • p p + n x 0 n. , .
  • , - n 0s. (, 0xFF n = 8, 0x7F 7 ..), 0 lef .
  • x . , x, 0
  • . 1s , 0s . , y, 0s .
  • y, . y, x, 0, , , .
  • , y, x y, |, - , . x, 0 (3), y. x, (3), 0s y (0s - 5), , x.
0

, , int IsBitSet (int i, int n); int SetBit (int i, int n); < → . , . .

0

2-6, , , , .

int setbits( int x, int p, int n, int y )
{
    int bitmask = ~0, y_right = 0; 

    /* Ok, first let get y rightmost bits.
     * Take our bitmask, shift it left by n bits, leaving the least significant
     * n bits as 0. Then, flip the bitmask so the rightmost n bits become 1, and the
     * leftmost n bits become 0.
     *
     * Then, AND this bitmask with y, to yield y n rightmost bits.
     */
    bitmask = ~( bitmask << n );
    y_right = bitmask & y;

    /*
     * Ok, now let use y_right as our bitmask for x.
     * Shift y_right to the left to *begin* at position p.
     */
     y_right <<= p - n;
     x |= y_right;

     return x;
}

: , , .

0

, :

0
0

All Articles