0

I have a 72 bit unsigned variable and I am only interested in 24 bits. The variable is split into the 34 bits / 24 bits / 14 bits. The bit mask works fine but I would like to dump the 34 zero's at the front and the 14 zero's at the back and store the result into a new variable.

I apologise for what may seem a very easy problem for most, I am only new to programming and have been thrown into a very very deep end. The project specifics Im dealing with are communicating an Anaren Radio module with the STM32F4Discovery Board. I have the peripherals on the STM board up and running with an SPI transfer function between the two devices. I am setting up the receive function to take the result from the radio module and isolate these specific bits. Then I'm sure there will be more and more questions but at the moment this is the wall Im beating my head against.

I sincerely thank everyone for their time and patience :) Sincerely, Hearny

EDIT: 1 Thanks all - usless info = never again. both vaiables are defined at beginning of the program, thought they would be irrelevant for the purpose of my question. After the & operation the result will give me the 24bits im interested in and 14 trailing zero's which I DONT WANT. How can I just drop them?

Code:

    anaren_packet=SPI1_send(0x00);  //send dummy packet to receive data
    packet_lap = anaren_packet & 3FFFFFC000; // Bitmask for middle-ish 24bits
auselen
  • 27,577
  • 7
  • 73
  • 114
Hearny
  • 17
  • 3
  • could you please give us the code definition of you data ? – Christophe Sep 05 '14 at 06:18
  • Welcome to stackoverflow. Please post some code, and please don't pad your question with useless fluff, no matter how well-intentioned it is. – user657267 Sep 05 '14 at 06:20
  • You'll need to find out how you receive the 72 bits in your program, which might be inherent in using some libraries provided with the module/Board or some instructions you use directly to interact with memory addresses or other I/O ports etc.. Anyway, when you have data into a C++ integral type, you can use the bit-shift operators `>>` `<<` combined with `&` and `|` to mask and recombine bit positions. You should write as much as you can and come back with actual code so we know specifically where you're stuck and what you're working with. – Tony Delroy Sep 05 '14 at 06:21

1 Answers1

2

The only problem in here is that 72 bit is larger than an int64_t, so you have to give you operate a thought and cannot just use trivial shifting and masking.

But you might want to use a bit field:

typedef union my_int72 {
    struct my_int72 {
        unsigned garbage_lsb : 14;
        unsigned actual_content : 24;
        unsigned garbage_msb : 34;
    } triple;
    uint8_t array[9];
} my_int72_t;

In here my_int72_t::triple and my_int72_t::array have the same size and are located on the same memory. To access the data you just read var_name.triple.actual_content.

The further advantage is that you can give the triple of data actually useful names. The downside is that often less than optimal code is generated for bit fields.


Edit: You are reading byte by byte, and you have to read 9 bytes, right?

Then just do:

 uint32_t result = 0;

 request_bytes();

 read_byte();                                    // byte 0
 result |= (uint8_t) read_byte() >>  6;          // byte 1 (2 bits read)
 result |= (uint8_t) read_byte() <<  2;          // byte 2 (10 bits read)
 result |= (uint8_t) read_byte() << 10;          // byte 3 (18 bits read)
 result |= ((uint8_t) read_byte() << 18) & 0x3F; // byte 4 (24 bits read)
 read_byte();                                    // byte 5
 read_byte();                                    // byte 6
 read_byte();                                    // byte 7
 read_byte();                                    // byte 8
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • Hmm this may throw a large spanner into my works then. I may have misinterpreted int64_t as a **64-byte** declaration not a **64-bit**. As the radio module transfers 64-byte data payloads over MOSI MISO channels... – Hearny Sep 05 '14 at 06:41
  • In `intX_t` the `X` is in bits because not every architecture has [`CHAR_BIT == 8`](http://stackoverflow.com/q/5516044). – Kijewski Sep 05 '14 at 06:44
  • So if my Radio module has a 64-byte FIFO scheme will it likely be transmitting in 64x8bit segments which I will have to separate into separate uint64_t arrays? Does that insinuate for a single 64-byte packet I will need 64 separate variables? – Hearny Sep 05 '14 at 06:58
  • @Hearny Not necessarily, but it's most common for such packets to have data elements which are cleanly split along 16- or 32-bit boundaries within the packet. – Sneftel Sep 05 '14 at 06:59
  • Anaren Manual - slighlty useless expect for specs and dimensions. [Anaren Manual](http://www.anaren.com/sites/default/files/user-manuals/A2500R24x_Users_Manual_0.pdf) Looking into the TI manual for the radio chip. Page 25 shows that the CC2500 TI chip transmits in byte-bursts, first in first out scheme. [TI Manual](http://www.ti.com/lit/ds/symlink/cc2500.pdf) – Hearny Sep 05 '14 at 07:11