2

I wish to have a list of bits where I can perform maths operations on a section of the list, for example.

value: 864
as bits, pos:   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
as bits, value: 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,  0

If I take bits 1 to 4 and 6 to 9 I get 0, 1, 1, 0, 1, 0, 0, 0 (104). Then if I add 3 it becomes 0, 1, 1, 0, 1, 0, 1, 1 (107).

I could do this with a std::vector and a process of involving bitwise operations between the bits, with code that looks like this sort of thing if(xor(data[n], data[n2])) {data[n - 1] == false;} etc...

I could even use POD arrays but I was thinking, C++ being so close to the internals of the computer is often not helpfull, but here it is, it could allow this operation to be done extremely quickly.

Something like storing the values in a big POD variable or an array and then copying them into the memory of another variable such that the mathmatical operations could be done before copying them back into the position in there storage again?

Even better could the for example addition operator somehow take that position in memory as a parameter to perform the addition directly into the middle of the store of data?

alan2here
  • 3,223
  • 6
  • 37
  • 62
  • 3
    It sounds like you want an `int`! – N_A Sep 20 '11 at 17:17
  • 1 to 4 and 6 to 9 (1, 2, 3, 4, 6, 7, 8, 9) was just an example, it could be any segment I need to be able to perform operations on. – alan2here Sep 20 '11 at 17:19
  • I'm not sure I entirely grasp what you want, but from what I understand, your best bet is either to copy your data into an `int` or to do Boolean logic in-place. The problem with referencing the bit array directly is that particular bits in your array won't always line up with the start of a byte. – N_A Sep 20 '11 at 17:26
  • possible duplicate of [How do you set, clear and toggle a single bit in C?](http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c) – Martin York Sep 20 '11 at 17:26
  • 1
    @Tux-D Not a dup. He wants to do math on arbitrary sections of his bit array, not on individual bits. – N_A Sep 20 '11 at 17:28

2 Answers2

1

It sounds like you want Boost.DynamicBitset:

The dynamic_bitset class represents a set of bits. It provides accesses to the value of individual bits via an operator[] and provides all of the bitwise operators that one can apply to builtin integers, such as operator& and operator<<. The number of bits in the set is specified at runtime via a parameter to the constructor of the dynamic_bitset.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • I think I would prefer std::bitset, which I didn't previously know about. It seems a similar but probbably prefrable solution to using an array of bool. It dosn't seem to allow me to choose a section from inside the bitset and perform an operation on it such as addition though if the section is more than 1 bit long without having to copy it out into something else the long way. – alan2here Sep 20 '11 at 17:41
  • I don't think the _processor_ is capable of performing any arbitrary operations on some (plural) bits in-place. Operations are usually done externally and then masked back to the source. – Mooing Duck Sep 20 '11 at 18:18
  • Sorry, wrote a full answer myself, ty for the information though. – alan2here Sep 20 '11 at 20:39
1

The possible fast way didn't work and would be verry limited.

unsigned int n = 8;
unsigned short *p1, *p2;
p1 = reinterpret_cast<unsigned short*>(&n);
p2 = reinterpret_cast<unsigned short*>((&n)+1);
std::cout<<*p1<<", "<<*p2<<std::endl;

Here is the full solution, thanks for the info that allowed me to complete this. It can easily be reworked to use std::vector instead.

#include <math.h> // for pow in weighted_random_UINT
#include <bitset> // or std::vector<bool>

template <std::size_t T> // this line is not required with std::vector
unsigned int get_chunk(std::bitset<T>* btset, unsigned int start, unsigned int end)
{
    unsigned long out = 0;
    for(unsigned int n = start; n < end; n++)
    {
        out += unsigned long((*btset)[n] * pow(2,double(n-start)));
    }
    return out;
}

void main()
{
    // 6 bits of data in reverse order
    std::bitset<6> data (std::string("000110"));

    // all the data numerically
    std::cout<<get_chunk<6>(&data,0,5)<<std::endl;

    // the first 3 bits as a number
    std::cout<<get_chunk<6>(&data,0,2)<<std::endl;

    // the last 3 bits as a number
    std::cout<<get_chunk<6>(&data,3,5)<<std::endl;
}
alan2here
  • 3,223
  • 6
  • 37
  • 62