445,655 Members | 1,005 Online Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

# bit manipulation question

 P: n/a I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 Any bit gurus here who can help me? Thanks, --Elijah Nov 13 '05 #1
13 Replies

 P: n/a ge******@hotmail.com (Elijah Bailey) writes: I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 I don't understand your k = 3 example. Shouldn't f(x) = 101 for consistency with the others? /* I think this'll do the trick, but I haven't tested it or even compiled it. In fact, I haven't even tried it out on paper, so you'd better think it through carefully before assuming anything. */ unsigned long f(unsigned long x, int k) { unsigned long out = 0; /* Output so far. */ unsigned long left = -1; /* Mask of bits left to extract from x. */ unsigned long msb = ~(left << 1 >> 1); /* Top bit in a unsigned long. */ do { /* Extract MSB of x into LSB of out. */ out <<= 1; if (x & msb) out |= 1; /* Advance. */ x <<= k; left <<= k; } while (left != 0); return out; } -- int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\ \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\ );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\ );}return 0;} Nov 13 '05 #2

 P: n/a Elijah Bailey wrote: I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 Any bit gurus here who can help me? Thanks, --Elijah Although this seems to me like a homework asignment, I couldn't resist to try something. I wouldn't try calling it as you do in your example, because I don't think any C compiler would compile it. This code is untested and does not take care any special cases. There is at least on obvious one, but you can take care of that yourself. long f(long x, int k) { long temp = 0; long i, j; for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j) temp |= (x & (1 << i)) >> (i - j); return temp; } Mark Nov 13 '05 #3

 P: n/a Capstar wrote: Elijah Bailey wrote: I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 Any bit gurus here who can help me? Thanks, --Elijah Although this seems to me like a homework asignment, I couldn't resist to try something. I wouldn't try calling it as you do in your example, because I don't think any C compiler would compile it. This code is untested and does not take care any special cases. There is at least on obvious one, but you can take care of that yourself. long f(long x, int k) { long temp = 0; long i, j; for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j) temp |= (x & (1 << i)) >> (i - j); return temp; } Mark Hmmm, after reading your post again, I seem to have made a little mistake. In my previous post you always start at bit 0. But when you say take every k-th bit, you obviously want to start at bit k - 1. I also tested it so I'll post my complete test program: #include #include #include char *bitprint(long x, unsigned long max) { unsigned long i, j; static char bitstring[sizeof(x) * 8 + 1]; if(max > sizeof(x) * 8) max = sizeof(x) * 8; for(i = max - 1, j = 0; j < max; --i, ++j) { if(x & (1 << i)) bitstring[j] = '1'; else bitstring[j] = '0'; } bitstring[max] = '\0'; return bitstring; } long f(long x, int k) { long temp = 0; unsigned long i, j; for(i = k - 1, j = 0; i < sizeof(x) * 8; i += k, ++j) temp |= (x & (1 << i)) >> (i - j); return temp; } int main(void) { long x = 0xaa; /*10101010*/ int k; char *xstring; xstring = strdup(bitprint(x, 8)); for(k = 1; k <= 4; ++k) printf("x = %s\tk = %d\t f(x, k) = %s\n", xstring, k, bitprint(f(x, k), 8)); free(xstring); return 0; } Mark PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall -ansi -pedantic I get: warning: implicit declaration of function `strdup'. Nov 13 '05 #4

 P: n/a PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall -ansi -pedantic I get: warning: implicit declaration of function `strdup'. No, strdup is *not* part of the standard. According to my manpage, conforms to SYS V R3 and BSD 4.3 -nrk. Nov 13 '05 #5

 P: n/a Ben Pfaff wrote: Elijah Bailey writes:I want to write a functionlong f(long x, int k)such that it extracts every k-th bit of x, concatenatesthem and returns it. Anyone can help me in writing thisfunction?examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 I don't understand your k = 3 example. Shouldn't f(x) = 101 for consistency with the others? Let x_i denote the i_th bit in x counting from right to left. In particular, x_0 denotes the right-most bit in x. Then f(x)_i = x_{k-1+i*k} k=1 => f(x)_i = x_i k=2 => f(x)_i = x_{2*i+1} k=3 => f(x)_i = x_{3*i+2} Thus, from right to left, x_2 | x_5 | x_8 Nov 13 '05 #6

 P: n/a On Wed, 03 Dec 2003 10:10:04 +0100 Capstar wrote: Capstar wrote: Elijah Bailey wrote: I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 Any bit gurus here who can help me? Thanks, --Elijah Although this seems to me like a homework asignment, I couldn't resist to try something. I wouldn't try calling it as you do in your example, because I don't think any C compiler would compile it. This code is untested and does not take care any special cases. There is at least on obvious one, but you can take care of that yourself. long f(long x, int k) { long temp = 0; long i, j; for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j) Tut, tut. Assuming CHAR_BIT==8 temp |= (x & (1 << i)) >> (i - j); return temp; } Mark Hmmm, after reading your post again, I seem to have made a little mistake. In my previous post you always start at bit 0. But when you say take every k-th bit, you obviously want to start at bit k - 1. I also tested it so I'll post my complete test program: #include #include #include char *bitprint(long x, unsigned long max) I would prefer to have the function take a parameter to the buffer to be used rather than returning a pointer to a static buffer. { unsigned long i, j; static char bitstring[sizeof(x) * 8 + 1]; Tut, tut. Still assuming CHAR_BIT==8 throughout this new version. if(max > sizeof(x) * 8) max = sizeof(x) * 8; for(i = max - 1, j = 0; j < max; --i, ++j) { if(x & (1 << i)) bitstring[j] = '1'; else bitstring[j] = '0'; } bitstring[max] = '\0'; return bitstring; } long f(long x, int k) { long temp = 0; unsigned long i, j; for(i = k - 1, j = 0; i < sizeof(x) * 8; i += k, ++j) temp |= (x & (1 << i)) >> (i - j); return temp; } int main(void) { long x = 0xaa; /*10101010*/ int k; char *xstring; xstring = strdup(bitprint(x, 8)); for(k = 1; k <= 4; ++k) printf("x = %s\tk = %d\t f(x, k) = %s\n", xstring, k, bitprint(f(x, k), 8)); free(xstring); return 0; } Mark PS. Isn't strdup in the standard? If I compile this with gcc -W -Wall -ansi -pedantic I get: warning: implicit declaration of function `strdup'. No, it's not part of the standard and so when you use -ansi -pedantic it is not defined by string.h -- Mark Gordon Paid to be a Geek & a Senior Software Developer Although my email address says spamtrap, it is real and I read it. Nov 13 '05 #7

 P: n/a Elijah Bailey wrote: I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 Any bit gurus here who can help me? Thanks, --Elijah Probably a bit of beating around the bush going on in the code below, but hey, you can verify your function as well :-) -nrk. #include #include #include unsigned long bitextract(unsigned long input, int k) { unsigned long ret = 0; size_t i; int j; if ( k <= 1 ) return input; for ( i = 0, j = 0; i < sizeof input * CHAR_BIT; i += k, ++j ) { input >>= (k-1); ret |= (input & 1) << j; input >>= 1; } return ret; } char *printBinary(char *buf, unsigned long num) { static char *hex2bin[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; size_t i; for ( i = 4; i <= (sizeof num * CHAR_BIT); i += 4 ) { int shift = (sizeof num * CHAR_BIT) - i; memcpy(buf, hex2bin[((num & (0xFUL << shift)) >> shift)], 4); buf += 4; } *buf = 0; return buf - (i - 4); } int verifyResult(int input, int k, int result) { char input_str[sizeof input * CHAR_BIT + 1]; char result_str[sizeof input * CHAR_BIT + 1]; char extract_str[sizeof input * CHAR_BIT + 1]; int i, j; printBinary(input_str, input); printBinary(result_str, result); for ( i = sizeof input_str - 1 - k, j = sizeof extract_str - 2; i >= 0; i -= k, --j ) { extract_str[j] = input_str[i]; } while ( j >= 0 ) extract_str[j--] = '0'; extract_str[sizeof extract_str - 1] = 0; return strcmp(result_str, extract_str); } int main(void) { unsigned long input = 0x12345678UL; unsigned long result; size_t i; for ( i = 1; i <= sizeof input * CHAR_BIT; ++i ) { result = bitextract(input, i); if ( verifyResult(input, i, result) ) { printf("Failed for bit %d\n", i); break; } } if ( i >= sizeof input * CHAR_BIT ) printf("Success!\n"); return 0; } Nov 13 '05 #8

 P: n/a Mark Gordon wrote: On Wed, 03 Dec 2003 10:10:04 +0100 Capstar wrote:Capstar wrote:Elijah Bailey wrote: I have a long x;I want to write a functionlong f(long x, int k)such that it extracts every k-th bit of x, concatenatesthem and returns it. Anyone can help me in writing thisfunction?examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11Any bit gurus here who can help me?Thanks,--Elijah Although this seems to me like a homework asignment, I couldn'tresist to try something.I wouldn't try calling it as you do in your example, because I don'tthink any C compiler would compile it.This code is untested and does not take care any special cases.There is at least on obvious one, but you can take care of thatyourself.long f(long x, int k){ long temp = 0; long i, j; for(i = 0, j = 0; i < sizeof(x) * 8; i += k, ++j) Tut, tut. Assuming CHAR_BIT==8 I am not a c.l.c. guru and so I'm not an allmighty know it all. And I thought sizeof returned in 8-bit bytes. I'll keep CHAR_BIT in mind next time. temp |= (x & (1 << i)) >> (i - j); return temp;}MarkHmmm, after reading your post again, I seem to have made a littlemistake. In my previous post you always start at bit 0. But when yousay take every k-th bit, you obviously want to start at bit k - 1.I also tested it so I'll post my complete test program:#include #include #include char *bitprint(long x, unsigned long max) I would prefer to have the function take a parameter to the buffer to be used rather than returning a pointer to a static buffer. I don't realy care wat you want. I just needed a function, which would return a character array so I could display the value of f(x,k) to verify the results easily. { unsigned long i, j; static char bitstring[sizeof(x) * 8 + 1]; Tut, tut. Still assuming CHAR_BIT==8 throughout this new version. I didn't became an allmighty know it all c.l.c. guru in those 32 minutes, So I still thought sizeof returned in 8-bit bytes. if(max > sizeof(x) * 8) max = sizeof(x) * 8; for(i = max - 1, j = 0; j < max; --i, ++j) { if(x & (1 << i)) bitstring[j] = '1'; else bitstring[j] = '0'; } bitstring[max] = '\0'; return bitstring;}long f(long x, int k){ long temp = 0; unsigned long i, j; for(i = k - 1, j = 0; i < sizeof(x) * 8; i += k, ++j) temp |= (x & (1 << i)) >> (i - j); return temp;}int main(void){ long x = 0xaa; /*10101010*/ int k; char *xstring; xstring = strdup(bitprint(x, 8)); for(k = 1; k <= 4; ++k) printf("x = %s\tk = %d\t f(x, k) = %s\n", xstring, k, bitprint(f(x,k), 8)); free(xstring); return 0;}MarkPS. Isn't strdup in the standard? If I compile this with gcc -W -Wall-ansi -pedantic I get: warning: implicit declaration of function`strdup'. No, it's not part of the standard and so when you use -ansi -pedantic it is not defined by string.h OK, thanks. Nov 13 '05 #9

 P: n/a On Wed, 03 Dec 2003 17:24:09 +0100 Capstar wrote: Mark Gordon wrote: Tut, tut. Assuming CHAR_BIT==8 I am not a c.l.c. guru and so I'm not an allmighty know it all. Nor am I. Perhaps I should not have said "Tut, tut" since it was not my intent to cause offence. And I thought sizeof returned in 8-bit bytes. I'll keep CHAR_BIT in mind next time. Well, I've been caught out a few times here. One of the good things about this group is that people do tend to pick up on the assumptions we (and I do deliberately include myself) make. I would prefer to have the function take a parameter to the buffer to be used rather than returning a pointer to a static buffer. I don't realy care wat you want. I just needed a function, which would return a character array so I could display the value of f(x,k) to verify the results easily. It was merely intended as a helpful stylistic comment, not an instruction. No, it's not part of the standard and so when you use -ansi -pedantic it is not defined by string.h OK, thanks. You're welcome. -- Mark Gordon Paid to be a Geek & a Senior Software Developer Although my email address says spamtrap, it is real and I read it. Nov 13 '05 #10

 P: n/a In article , Grumble wrote:Ben Pfaff wrote: Elijah Bailey writes:I want to write a functionlong f(long x, int k)such that it extracts every k-th bit of x, concatenatesthem and returns it. Anyone can help me in writing thisfunction?examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 I don't understand your k = 3 example. Shouldn't f(x) = 101 for consistency with the others?Let x_i denote the i_th bit in x counting from right to left.In particular, x_0 denotes the right-most bit in x.Then f(x)_i = x_{k-1+i*k}k=1 => f(x)_i = x_ik=2 => f(x)_i = x_{2*i+1}k=3 => f(x)_i = x_{3*i+2}Thus, from right to left, x_2 | x_5 | x_8 Unless I've misunderstood, if x_0 is the right-most bit in x, that would make x_8 one to the left of the left-most bit in x. For the example given, this doesn't exist. -- Michael Nov 13 '05 #11

 P: n/a Mark Gordon wrote: On Wed, 03 Dec 2003 17:24:09 +0100 Capstar wrote:Mark Gordon wrote: Tut, tut. Assuming CHAR_BIT==8I am not a c.l.c. guru and so I'm not an allmighty know it all. Nor am I. Perhaps I should not have said "Tut, tut" since it was not my intent to cause offence. OK, well then no offence taken. It was indeed the "Tut, tut" that pissed me off. I normally don't mind any comment on my code, I even welcome it. I would prefer to have the function take a parameter to the bufferto be used rather than returning a pointer to a static buffer.I don't realy care wat you want. I just needed a function, which wouldreturn a character array so I could display the value of f(x,k) toverify the results easily. It was merely intended as a helpful stylistic comment, not an instruction. I know. Sorry, I normally wouldn't react like that. (Was still pissed). But your solution indeed has it's advantages. Mine isn't useable more than once in a printf statement for instance. That's why I did: xstring = strdup(bitprint(x, 8)); Mark. Nov 13 '05 #12

 P: n/a ge******@hotmail.com says... I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? examples x = 10101010 k = 1 f(x) = 10101010 x = 10101010 k = 2 f(x) = 1111 x = 10101010 k = 3 f(x) = 010 x = 10101010 k = 4 f(x) = 11 Any bit gurus here who can help me? Its not portable, but I'm sure you can adapt it to your platform of choice: long f(long x, int k) { switch (k) { case 1: return x; case 2: x &= 0xAAAAAAAA; x = (x | (x >> 1)) & 0x66666666; x = (x | (x >> 2)) & 0x1E1E1E1E; x = (x | (x >> 4)) & 0x01FE01FE; x = (x | (x >> 8)) & 0x0001FFFE; return x >> 1; case 3: x &= 0x24924924; x = (x | (x >> 2)) & 0x0C30C30C; x = (x | (x >> 4)) & 0x0C03C03C; x = (x | (x >> 8)) & 0x0C0003FC; x = (x | (x >> 16)) & 0x00000FFC; return x >> 2; case 4: x &= 0x88888888; x = (x | (x >> 3)) & 0x18181818; x = (x | (x >> 6)) & 0x00780078; x = (x | (x >> 12)) & 0x000007F8; return x >> 3; case 5: x &= 0x21084210; x = (x | (x >> 4)) & 0x0300C030; x = (x | (x >> 8)) & 0x030000F0; x = (x | (x >> 16)) & 0x000003F0; return x >> 4; case 6: x &= 0x20820820; x = (x | (x >> 5)) & 0x20060060; x = (x | (x >> 10)) & 0x200001E0; x = (x | (x >> 20)) & 0x000003E0; return x >> 5; case 7: x &= 0x08102040; x = (x | (x >> 6)) & 0x003000C0; x = (x | (x >> 12)) & 0x000003C0; return x >> 6; case 8: x &= 0x80808080; x = (x | (x >> 7)) & 0x01800180; x = (x | (x >> 14)) & 0x00000780; return x >> 7; case 9: x &= 0x04020100; x = (x | (x >> 8)) & 0x04000300; x = (x | (x >> 16)) & 0x00000700; return x >> 8; case 10: x &= 0x20080200; x = (x | (x >> 9)) & 0x20000600; x = (x | (x >> 18)) & 0x00000E00; return x >> 9; case 11: x &= 0x00200400; x = (x | (x >> 10)) & 0x00000C00; return x >> 10; case 12: x &= 0x00800800; x = (x | (x >> 11)) & 0x00001800; return x >> 11; case 13: x &= 0x02001000; x = (x | (x >> 12)) & 0x00003000; return x >> 12; case 14: x &= 0x08002000; x = (x | (x >> 13)) & 0x00006000; return x >> 13; case 15: x &= 0x20004000; x = (x | (x >> 14)) & 0x0000C000; return x >> 14; case 16: x &= 0x80008000; x = (x | (x >> 15)) & 0x00018000; return x >> 15; case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: return (x >> (k-1)) & 1; default: break; } return 0; } -- Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/ Nov 13 '05 #13

 P: n/a Michael Andrew Fyles wrote: In article , Grumble wrote:Ben Pfaff wrote:Elijah Bailey writes: I want to write a functionlong f(long x, int k)such that it extracts every k-th bit of x, concatenatesthem and returns it. Anyone can help me in writing thisfunction?examplesx = 10101010 k = 1 f(x) = 10101010x = 10101010 k = 2 f(x) = 1111x = 10101010 k = 3 f(x) = 010x = 10101010 k = 4 f(x) = 11 I don't understand your k = 3 example. Shouldn't f(x) = 101 forconsistency with the others?Let x_i denote the i_th bit in x counting from right to left.In particular, x_0 denotes the right-most bit in x.Then f(x)_i = x_{k-1+i*k}k=1 => f(x)_i = x_ik=2 => f(x)_i = x_{2*i+1}k=3 => f(x)_i = x_{3*i+2}Thus, from right to left, x_2 | x_5 | x_8 Unless I've misunderstood, if x_0 is the right-most bit in x, that would make x_8 one to the left of the left-most bit in x. For the example given, this doesn't exist. You are correct. Nov 13 '05 #14

### This discussion thread is closed

Replies have been disabled for this discussion. 