How to get half of the last binary number

For example if we have 101010
First half is 101 which you get by multiplying 101010 * 10 ^-3

now how do you get the second half (010) ??

Thank you
Just subtract the first half from the whole thing, remembering to add trailing zeroes:

101010 * 10^(-3) = 101

101010 - 101000 = 010
convert it to a string is what I do. I templated the conversion function, so it only has to be written once (instead of written for every single data type conversion needed).

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <sstream>

//.......

template<class type1, class type2>
type2 conv(const type1& t1)
{
    stringstream ss;
    type2 t2;
    ss<< t1;
    ss>> t2;
    return t2;
}


next, you can just cut up the string the way you wish, and convert it back.
Topic archived. No new replies allowed.