I have two pointer. one is input ptr and another one is for output..
My input pointer contains the binary values as
15.................0
0011 0110 1111 0111 - unsigned 16 bit
1011 0100 1011 1100 - unsigned 16 bit
First i want to take even poistion bits from both rows and have to fill this into the output pointer.
o/p pointer will be
0110 0110 0110 1111 (even position bits from both)..from right to left and assume 0 to 15.
1100 1110 0101 1101 (odd position bits from both)
how to do it by using pointers?? I am very new to pointers
Transforming your code to use pointers is pretty simple usually.
Take the example:
uint16_t compute(uint16_t a, uint16_t b)
{
return ((a | b) & MY_BIT_MASK) >> (a & 2); // some arbitrary operations
}
uint16_t compute_p(uint16_t *a, uint16_t *b)
{
return ((*a | *b) & MY_BIT_MASK) >> (*a & 2); // some arbitrary operations via pointers
}
If you show us your actual code, we can perhaps help you more. But basically, when you have a pointer to uint16_t, you can use it in two ways:
uint16_t *p;
p <- to mean the pointers value, which is an address, you assign it to other pointers, or increment it, so it would point to the next uint16_t in memory
*p <- to use it in expressions on place of a uint16_t, this way your code will each time fetch the actual 16 bit value pointed to by p
p[i] <- with int i, This is basically a synonym to *(p+i) as long as i is non-negative
*(p + i) <- with int i, This is also the value of 16 bit integer where p points to, or the next, or previous etc 16 bit value. In this form, negative i can be used as well
p - q <- with uint16* q the difference of the pointers, in case of uint16_t * types, this basically means "how many 16 but ints I can store and addresses q, q+1, q+2, q+3 .... p-1", assuming p>q
As far as I know, you cannot do what you desire with pointers in C. This is because, as far as I know, you cannot make pointers to point bits in C, they can only point to bytes.
One solution that I can come up with is to use bit masks. By masking individual bits, shifting them into the positions that you want, and assigning them into a new blank variable could achieve what you want. Here, examine the following:
#define bitmask(_x_) (1 << (_x_))
#include <stdio.h>
#include <inttypes.h>
int main( ){
uint16_t in1 = 14071; // 0011 0110 1111 0111
uint16_t in2 = 46268; // 1011 0100 1011 1100
uint16_t out1 = 0;
uint16_t out2 = 0;
for ( int i = 0; i < 8; i++ ) {
out1 |= ( in1 & bitmask( i * 2 ) ) >> i;
out1 |= ( ( in2 & bitmask( i * 2 ) ) >> i ) << 8;
out2 |= ( in1 & bitmask( i * 2 + 1 ) ) >> ( i + 1 );
out2 |= ( ( in2 & bitmask( i * 2 + 1 ) ) >> ( i + 1 ) ) << 8;
}
printf( "out1: %u\nout2: %u", out1, out2 );
return 0;
}
The output is:
out1: 26223
out2: 52829
Which have the form that you want in base 2.
Edit: Alternate version with the same output, this may be somewhat more reader-friendly, I have no idea about the performance aspect:
#define bitMASK(maskee,bit) (((maskee) >> (bit)) & 1)
#include <stdio.h>
#include <inttypes.h>
int main( ){
uint16_t in1 = 14071; // 0011 0110 1111 0111
uint16_t in2 = 46268; // 1011 0100 1011 1100
uint16_t out1 = 0;
uint16_t out2 = 0;
for ( int i = 0; i < 8; i++ ) {
out1 |= bitMASK( in1, 2 * i ) << i;
out1 |= bitMASK( in2, 2 * i ) << ( 8 + i );
out2 |= bitMASK( in1, 2 * i + 1 ) << i;
out2 |= bitMASK( in2, 2 * i + 1 ) << ( 8 + i );
}
printf( "out1: %u\nout2: %u", out1, out2 );
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.