How to correctly create a bit mask for a number

Tell me in simple human language - how to correctly compose and use bit masks? Preferably with examples in C.

For example, I have an int of 6 digits (if we consider the decimal representation), say 112 300. I need to allocate the highest digits of the number (one hundred and ten thousand) or vice versa (two thousand three hundred).

There is an understanding that the bitwise operation ' And ' ironically gives '1' when the bits match, but there is no understanding (or confused) of that, how to properly design the mask and perform a shift for the selection.

God grant you patience, thank you!

Author: p4sh, 2016-11-06

2 answers

Bit operations are designed to work with bits. There is no match between the decimal digits and the bitmask. There is a correspondence between hexadecimal digits and the bitmask.

You just need to divide the number by a power of 10 or take the remainder of the division by a power of 10 to get one or another part of the number.

For example, to get 2300 from the number 112 300 , you need to write the expression

112300 % 10000

As for the shift operators >> and

For example, if you have a number 3 that can be represented in binary form as 0011, then after applying the >> operator to this number, you will get 0001, that is, 1. Indeed, 3 / 2 is equal to 1 for integer operands. If you use the 0110, which corresponds to 6.

As for bitmaps It is very easy to write a function that provides a mask for allocating some continuous portion of the bits of a number.

For example

#include <iostream>
#include <iomanip>

unsigned int get_mask( unsigned int pos, unsigned int n )
{
    return ~( ~0 << n ) << pos;
}   

int main() 
{
    unsigned int x = 0xAB;
    unsigned int mask = get_mask( 0, 4 );

    std::cout << std::hex << ( x & mask ) << std::endl;

    mask = get_mask( 4, 4 );

    std::cout << std::hex << ( x & mask ) << std::endl;

    return 0;
}

Program output to the console

b
a0

That is, in the first case of applying the mask, the lower 4 bits were allocated. The other bits of the number are reset to zero. In the second case, the next 4 bits of the number were allocated, and the other bits were reset to zero.

Or a more expressive program

#include <iostream>
#include <iomanip>

unsigned int get_mask( unsigned int pos, unsigned int n )
{
    return ~( ~0 << n ) << pos;
}   

int main() 
{
    unsigned int x = 0x12345678;

    for ( size_t i = 0; i < 2 * sizeof( unsigned int ); i++ )
    {
        unsigned int mask = get_mask( 4 * i, 4 );

        std::cout << std::hex << ( x & mask ) << std::endl;
    }

    return 0;
}

Its output to the console

8
70
600
5000
40000
300000
2000000
10000000
 7
Author: Vlad from Moscow, 2016-11-06 20:50:44

Bitmasks have nothing to do with your task.

I have an int of 6 characters

"int in 6 characters" does not happen. There is a int size of N bits, where N depends on the specific hardware. "How many characters" is in the string representation of a number in a certain number system.

And if we are talking about the decimal system, then

I need to allocate the highest digits of the number (one hundred and ten thousand)

112300 / 10**4

Or vice versa (two thousand three hundred).

112300 % 10**4

In general, when it comes to the positional number system, then divide or take the remainder from the expression основание_СС в степени "позиция нужного разряда".

 4
Author: PinkTux, 2016-11-06 22:08:28