簡體   English   中英

如何使用C將奇數和偶數位置位存儲到另一個指針?

[英]How to store odd and even position bits to another pointer using C?

我有兩個指針。 一個是輸入ptr,另一個是輸出。

我的輸入指針包含二進制值

15.................0
 0011 0110 1111 0111 - unsigned 16 bit
 1011 0100 1011 1100 - unsigned 16 bit

首先,我想從兩行中獲取偶數位,並且必須將其填充到輸出指針中。

o/p 指針將是

  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)

如何通過使用指針來做到這一點? 我對指針很陌生

轉換您的代碼以使用指針通常非常簡單。

舉個例子:

 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
 }

如果您向我們展示您的實際代碼,我們或許可以為您提供更多幫助。 但基本上,當你有一個指向 uint16_t 的指針時,你可以通過兩種方式使用它:

 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

據我所知,你不能用 C 中的指針做你想做的事。這是因為,據我所知,你不能在 C 中創建指向位的指針,它們只能指向字節。

我能想到的一種解決方案是使用位掩碼。 通過屏蔽單個位,將它們移動到您想要的位置,並將它們分配到一個新的空白變量中,可以實現您想要的。 在這里,檢查以下內容:

#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;
}

輸出是:

out1: 26223
out2: 52829

其中具有您想要的基數 2 的形式。

編輯:具有相同輸出的替代版本,這可能對讀者更友好,我不知道性能方面:

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM