V
V
Vadim kyklaed2018-06-05 09:45:44
C++ / C#
Vadim kyklaed, 2018-06-05 09:45:44

What is the point of working bit by bit with decimal numbers?

Hello, please explain what is the essence and how to understand the work bit by bit with decimal numbers?
there is a program, it must take a certain number of bits from a certain place
for a binary number example. if I understand correctly.
x= 10101111
p=2
n=2
1)x>>(p+1-n)
x>> 1 = 01010111
2)~(~0 shift left n)
~0 = 11111111
11111111<<2 = 11111100
~11111100= 00000011
3)01010111 AND(bitwise) 00000011 = 00000011
response 00000011

#include <stdio.h>
#include  <conio.h>

unsigned getbits(unsigned x, int p, int n){
 return (x >> (p+1-n)) & ~(~0 << n);
}

void main(){
 printf("%d",getbits(1010,2,3));
 getch();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-06-05
@kyklaed

Bit access is used, usually to store and test some set of flags. Those. each bit of some variable is a flag, if the bit is equal to 1 - the flag is set, if 0 - it is reset.
The result of the getbits function can be interpreted as a logical sign - whether a certain flag is set or not. In this case, the call to getbits can be hidden behind macros that will substitute the appropriate parameters in getbits depending on which flag is checked.
The same can be solved with a structure with bit fields, in which case the compiler will take care of all the work with bits. The result in this case will be approximately the same as in the case of manual bit manipulation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question