Bit offset in C

Recently, I decided to undertake an SMS project to send and receive SMS, although mobile.

Data is sent in PDU format - I need to change the ASCII characters to 7-bit characters of the GSM alphabet. For this, I came across several examples, such as http://www.dreamfabric.com/sms/hello.html

This example shows the rightmost bits of the second septet that are inserted into the first septet to create an octet.

Bitwise shifts do not cause this to happen, as → will be inserted to the left, and <to the right. As far as I understand, I need something bitwise rotation to create this - can someone tell me how to move bits from the right hand and insert them on the left?

Thank,

+3
source share
2 answers

Here is a quick algorithm for this:

int b1, bnext;
int mod;
int pand;
char *b; // this is your byte buffer, with message content
int blen; // this is your byte buffer length
char sb[160];
int totchars = 0;

b1 = bnext = 0;
for (int i=0; i < blen; i++) {
    mod = i%7;
    pand = 0xff >> (mod + 1);
    b1 = ((b[i] & pand) << mod) | bnext;
    bnext = (0xff & b[i]) >> (7 - mod);
    sb[totchars++] = (char)b1;
    if (mod == 6) {
        sb[totchar++] = (char)bnext;
        bnext = 0;
    }
}
sb[totchar] = 0;

It converts 7-bit compressed buffers into regular ASCII char arrays in C.

+3
source

can someone tell me how to move bits from the right hand and insert them to the left?

There are indirect paths in C, but I just did it like this:

void main()
{
    int x = 0xBAADC0DE;
    __asm
    {
        mov eax, x;
        rol eax, 1;
    }
}

This will rotate (not shift!) The bits to the left (one step). "ror" will rotate to the right.

0
source

Source: https://habr.com/ru/post/1740670/


All Articles