K
K
Kirill Zhilyaev2018-02-11 01:02:01
Mathematics
Kirill Zhilyaev, 2018-02-11 01:02:01

How to pack numbers without bit operations?

There are 4 bytes of space. There are several polbyte numbers. How to pack half-byte numbers into a 4-byte number using arithmetic operations?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-02-11
@kirill_782

How to pack half-byte numbers into a 4-byte number using arithmetic operations?

Multiply/divide by 16 i and add.
For example:
uint32_t put_nibble(uint32_t v, int nibble_index, uint32_t nibble)
{
    uint32_t p = 1;
    uint32_t r;
    int i;
    for (i = 0; i < nibble_index; ++i)
        p *= 16;
    r = (v % p) + (nibble * p);
    if (nibble_index < 7)
        r += (v / (p * 16)) * p * 16;
    return r;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question