0

I am given a 32-bit unsigned integer and I am trying to save the 8 most significant bits from that unsigned integer into an unsigned char. I then want to clear those bits to 0s. That last part can be done with a mask but I am unsure of how exactly to find the 8 most significant bits and then having the mask know exactly which bits to clear.

Another related problem is that I am trying to find the least significant bit and shift it to the left to make it the most significant and return that value. Like above I am unsure on how to find the least significant bit and then know how far to shift it to the left

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Does [this question](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit?rq=1) help? – Carl Norum May 20 '20 at 22:38
  • `num &= 0x00ffffff` will clear the 8 most significant bits. – Barmar May 20 '20 at 22:40
  • 1
    Are you sure you want the 8 most significant bits or the 8 most significant non-zero bits? – Doug Currie May 20 '20 at 22:41
  • Same question about LSB – Doug Currie May 20 '20 at 22:41
  • I want the 8 most significant non-zero bits. For the least, it is the first one zero bit as well. – PhantomProgramer May 20 '20 at 22:49
  • Update: I am also given the number of bits present. So if I am passed the number 16, I know that the first 16 are all 0s and the last 16 have ether 1s or 0s. I need the first 8 of that chunk that depends on the given number. Hope that makes sense. – PhantomProgramer May 20 '20 at 22:55
  • Find the leftmost `1` bit. Use that and the seven bits following it as the leftmost nonzero bits? – wildplasser May 20 '20 at 22:58
  • I'm having a hard time following the issue(s) you're having. If you need the 8 most significant bits, @DougCurrie has the answer.Can you explain what the issue is as well as provide the code you've written so far? – Burstful May 20 '20 at 23:02
  • Not quite. For example, if I am given the number 16 and 00000000 00000000 01010010 00101010 then I want the chunk 01010010. The problem is I dont know what number is given in order to find and clear the chunk I want – PhantomProgramer May 20 '20 at 23:06
  • So you're given a uint32_t and a number ( say n ).. so you're wanting to basically take the uint32_t trim everything except the least significant n bits.. and then return the most significant 8 bits of the new n bits? – Burstful May 20 '20 at 23:10
  • yes that sounds right – PhantomProgramer May 20 '20 at 23:16
  • Does this answer your question? [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – Daniel Walker May 20 '20 at 23:17

1 Answers1

0

You can write two general functions one of which return the specified byte of an object of the type unsigned character and other returns a number with cleared bits of the number used as an argument.

Here is a demonstrative program.

#include <stdio.h>
#include <limits.h>

unsigned char extract_byte( unsigned int x, size_t n )
{
    n %= sizeof( x );

    return  x >> CHAR_BIT * n & 0xFF;
}

unsigned int clear_byte( unsigned int x, size_t n )
{
    n %= sizeof( x );

    return x & ~( 0xFF << n * CHAR_BIT );
}

int main(void) 
{
    unsigned int x = 0x04030201;

    for ( size_t i = 0; i < sizeof( x ); i++ )
    {
        printf( "%hhu ", extract_byte( x, i ) );
    }
    putchar( '\n' );

    for ( size_t i = 0; i < sizeof( x ); i++ )
    {
        printf( "%#x ", x = clear_byte( x, i ) );
    }
    putchar( '\n' );

    return 0;
}

Its output is

1 2 3 4 
0x4030200 0x4030000 0x4000000 0 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335