Search code examples
c++binaryconcatenationarduino-c++

How to concatenate binary macro values? What is the binary type?


Working on an arduino project that uses binary command codes.

Sample Rate and Filter Type are 2 codes that are concatenated. Sample Rate= MSB half and Filter Type = LSB half.

found: Bitwise concatenation in C

#include <stdio.h>
int main() {

    int first = 0b1010;
              //^^v See here
    int second = 0b0011;
    int result = (first << 4) | second;
    printf("%d", result);

    return 0;

Which shows how to concatenate two binary values and add them.

Have all the binary codes in the .h file as macros. Can I do the same for pure binary values? What is the type? Will use the concatenated binary value - as a command in a function.

Usage of binary code command currently Looks Like

All the Macros:

    //Sample Rate and Filter Type Codes From ADS1261 Datasheets
/*Binary Code Commands Defining Sampling Rate and Filter Type - http://www.ti.com/lit/ds/symlink/ads1261.pdf#page=61*/

 //ADC DATA RATE
#define SPS_2_PT_5        0b00000  //SPS = 2.5
#define SPS_5             0b00001  //SPS = 5
#define SPS_10            0b00010  //SPS = 10
#define SPS_16_PT_6       0b00011  //SPS = 16.6
#define SPS_20_DEFAULT    0b00100  //SPS = 20 <--- this is also the default.
#define SPS_50            0b00101  //SPS = 50
#define SPS_60            0b00110  //SPS = 60
#define SPS_100           0b00111  //SPS = 100
#define SPS_400           0b01000  //SPS = 400
#define SPS_1200          0b01001  //SPS = 1200
#define SPS_2400          0b01010  //SPS = 2400
#define SPS_4800          0b01011  //SPS = 4800
#define SPS_7200          0b01100  //SPS = 7200
#define SPS_14400         0b01101  //SPS = 14400
#define SPS_19200         0b01110  //SPS = 19200
#define SPS_25600         0b00110  //SPS = 25600
#define SPS_40000         0b10000  //SPS (f_CLK - 10.24 MHz)

//Define Filter Types
#define F_SINC1 0b000 //SINC1
#define F_SINC2 0b001 //SINC2
#define F_SINC3 0b010 //SINC3
#define F_SINC4 0b011 //SINC4
#define F_FIR   0b100 //FIR// Default if not specified.

Solution

  • It would be exactly the same code. Macros would be substitute with binary literals, literals would be promoted to integers, and then left shift followed by or operators will be called.