-4

I have a hexadecimal value 0x40BF00FF and i want to get this value 0xFF (last two number) from it and other numbers are ignore. how can we write code in C language

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194

4 Answers4

2

Solution

Use bit masking with 0x000000FF:

int result = 0x000000FF & 0x40BF00FF;

full code example

int input = 0x40BF00FF; //input example
int output = 0x000000FF & input;
printf("%04x", output); //prints result

Result

00ff
ibezito
  • 5,782
  • 2
  • 22
  • 46
1
uint8_t get8bits(uint32_t value, int byteNumber) 
{
 uint8_t bitShift = byteNumber * 8;
 uint32_t mask = 0xfful << byteShift;

 return (value & mask) >> bitShift;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

If you are having a hex value (not a string). This small program can be a good example:

This method extracts the individual nibble. And then joins them back.

We can get the least significant 4-bits by taking modulus with 16. Then we can do left shift 4 times (divide by 16). And then extract the next least significant 4-bits.

Now we got the 8 LSB. Now we just append them.

#include <stdio.h>

int main()
{
    unsigned int x = 0x40BF00FF;

    // Extracting the first 4 least significant bits.
    unsigned int one = x % 16;

    // Extracting the next 4 least significant bits.
    unsigned int two = (x >> 4) % 16;

    // Appending the bits and printing the result.
    printf ("%x\n", (two << 4) + one);

    return 0;
}

OR

You can use the power of bits!

Here, the input is stored in x.

I take a mask 0xFF.

And I perform bitwise and operation.

Now, as we know

b&1 = b and b&0 = 0.

We can use this property to extract only the lower 8 bits by setting them all to 1 and others as 0. And performing a bitwise and (&).

#include <stdio.h>

int main()
{
    unsigned int x = 0x40BF00FF;

    // The mask.
    unsigned int mask = 0xFF;

    // Applying bitwise and
    printf ("%x\n", x & mask);

    return 0;
}
NVS Abhilash
  • 574
  • 7
  • 24
  • explain how code work? – Ahmad Raza Jul 02 '17 at 08:46
  • @AhmadRaza I have made some changes to answer, is it fine now? – NVS Abhilash Jul 02 '17 at 08:59
  • This code doesn't work. @NVS Abhilash unfortunately does not know the C good enough :) _*unsigned int x = 0x40BF00FF*_ assigns 0xff to the x as it is 16 bits value. This is the arduino and it uses (in most models) 8 bit AVR uCs. – 0___________ Jul 02 '17 at 12:41
  • @PeterJ Thank you for pointing it out. Any suggestions how can I correct my code? (Cause the hexadecimal that the questioner gave is of 32-bit value. It can be stored in 2 registers like `RL` and `RH`. And we can use the above code for `RL`. Any better suggestions? – NVS Abhilash Jul 02 '17 at 14:06
  • Believe in the compiler. Just use type which is big enough to accommodate your value. In this case uint32_t instead of uinsigned int which is 16 bits on the 8 bit machines, BTW AVRs do not have BL or BH registers, the general use registers are called R1, R2.... R31. The function in my answer returns any byte from the 32 number (eg 0, 1, 2 or 3). Literals which are > 16 bits have to be followed by l or ul postfix. – 0___________ Jul 02 '17 at 15:42
-1

It depends on the version of language that you are using. But for more you can read here

After you substring the two last last characters than you can concatenate it with first character.

Orges_Kreka
  • 41
  • 1
  • 5