#define EXTRACT_MASK (1 << 29) | (1 << 28) | (1 << 27) | (1 << 13) | (1 << 11)

unsigned char uc[4];

unsigned long ul = *(unsigned long*) uc;

unsigned long result = ul & EXTRACT_MASK;

printf("Result: %u\n", result)

Solved

Posted on 2006-06-05

I have a 4 byte unsigned char. I am taking the 1st byte, and extracting bits 29 through 27 as well as 13 and 11.

I need to then take each set of 3 bits and get the decimal value of it.

Is there a graceful way of doing this other than extracting each bit manually then calculating the decimal value?

I need to then take each set of 3 bits and get the decimal value of it.

Is there a graceful way of doing this other than extracting each bit manually then calculating the decimal value?

10 Comments

#define EXTRACT_MASK (1 << 29) | (1 << 28) | (1 << 27) | (1 << 13) | (1 << 11)

unsigned char uc[4];

unsigned long ul = *(unsigned long*) uc;

unsigned long result = ul & EXTRACT_MASK;

printf("Result: %u\n", result)

>>I need to then take each set of 3 bits and get the decimal value of it.

#define EXTRACT_MASK1 (1 << 29) | (1 << 28) //having bit value 1 at position 29 and 28, and other places filled 0's

#define EXTRACT_MASK2 (1 << 27) | (1 << 13) | (1 << 11)

unsigned char uc[4];

unsigned long ul = *(unsigned long*) uc;

printf("Result: %u %u\n", (ul & EXTRACT_MASK1), (ul & EXTRACT_MASK2))

You said 13 and 11 and then you mention "each set of 3 bits", so i assume you meant 13 to 11 too.

Here's a modified solution based on jkr's:

#define EXTRACT_MASK (7 << 11)|(7<<27)

unsigned char uc[4];

unsigned long ul = *(unsigned long*) uc;

unsigned long result = ul & EXTRACT_MASK;

unsigned char group_11_13 = (result >> 11) & 7;

unsigned char group_27_29 = (result >> 27) & 7;

printf("%u %u", group_11_13, group_27_29);

#define EXTRACT_1(ul) (((ul) & 0x38000000) >> 27)

#define EXTRACT_2(ul) (((ul) & 0x00003800) >> 11)

unsigned char uc[4];

unsigned long ul = *(unsigned long*) uc;

printf("Result: %u %u\n", EXTRACT_1(ul), EXTRACT_2(ul))

Hi jewee , chk out this code ..enter any binay no. and u will get its decimal, hexadecimal as well as octal representation..

cheers.

prog starts below

//-----------------

#include <stdio.h>

#include <string.h>

int bin2dec(char *bin);

int main()

{

char bin[80] = "";

char *p;

int dec;

while(strcmp(bin,"0"))

{

printf("\n Enter a binary number (just 0 to EXIT): ");

fgets(bin, sizeof(bin), stdin);

// check for and remove trailing \n

if ((p = strchr(bin,'\n')) != NULL)

{

*p = '\0';

}

dec = bin2dec(bin);

if (dec) printf("\nDecimal = %d Hexadecimal = 0x%04X Octal = 0%o\n",dec,dec,dec);

}

getchar(); // wait

return 0;

}

// convert a binary string to a decimal number, returns decimal value

int bin2dec(char *bin)

{

int b, k, m, n;

int len, sum = 0;

len = strlen(bin) - 1;

for(k = 0; k <= len; k++)

{

n = (bin[k] - '0'); // char to numeric value

if ((n > 1) || (n < 0))

{

puts("\n\n ERROR! BINARY has only 1 and 0!\n");

return (0);

}

for(b = 1, m = len; m > k; m--)

{

// 1 2 4 8 16 32 64 ... place-values, reversed here

b *= 2;

}

// sum it up

sum = sum + n * b;

//printf("%d*%d + ",n,b); // uncomment to show the way this works

}

return(sum);

}

unsigned char data[4];

///fill value in this char data

dec1=(data[0] & 0x1c)>>2;

dec2=(data[2] & 0x1c)>>2;

In binary, 0x1c is 001 1100. So for the first byte, this mask will get you bits in positions 29-27 and in the third byte, it will get you bits 13-11.

The "shift right" two bits to get the decimal value.

HTH.

Padam JS.

Bit counting starts from 0, so you need 0x38 (and a shift of 3) as in my earlier example.

Bit counting indeed starts at 0.

However, 0x1c>>2 is equal to 0x38>>3!

Also, you are generating a mask with 0x38, which very well can be done using 0x1c.

I am not generating any mask, and hence I can just And with the mask 0x1c.

Title | # Comments | Views | Activity |
---|---|---|---|

merging MP3 audio files | 10 | 79 | |

convert char array to number in c | 5 | 66 | |

Windows 10 Sysprep Issues | 1 | 152 | |

Choppy program performance on Windows Server 2012 | 20 | 66 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**16** Experts available now in Live!