Subversion Repositories HomeAutomation

Rev

Rev 1597 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1596 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Bitset.h"
22
 
23
#include <math.h>
24
#include <string.h>
25
 
26
namespace atom {
27
namespace type {
28
 
29
Bitset::Bitset(unsigned int count)
30
{
31
    this->count_ = count;
32
    this->bytes_ = new unsigned char[(int)ceil(count / 8)];
33
}
34
 
35
Bitset::Bitset(const Byteset& set)
36
{
37
    this->count_ = set.GetSize() * 8;
38
    this->bytes_ = new unsigned char[set.GetSize()];
39
 
40
    memcpy(this->bytes_, set.Get(), set.GetSize());
41
}
42
 
43
Bitset::~Bitset()
44
{
45
    delete [] this->bytes_;
46
}
47
 
48
unsigned int Bitset::GetCount()
49
{
50
    return this->count_;
51
}
52
 
53
unsigned long Bitset::Read(unsigned int position, unsigned int length)
54
{
55
    unsigned long value = 0;
56
 
57
    for (unsigned int index = 0; index < length; index++)
58
    {
59
        value = (value << 1) | this->Get(position + index);
60
    }
61
 
62
    return value;
63
}
64
 
65
int Bitset::Set(unsigned int position)
66
{
67
    if (position >= this->count_)
68
    {
69
        return -1;
70
    }
71
 
72
    this->bytes_[position / 8] |= (0x00000001 << (7 - position));
73
 
74
    return 0;
75
}
76
 
77
int Bitset::Unset(unsigned int position)
78
{
79
    if (position >= this->count_)
80
    {
81
        return -1;
82
    }
83
 
84
    this->bytes_[position / 8] &= ~(0x00000001 << (7 - position));
85
 
86
    return 0;
87
}
88
 
89
int Bitset::Get(unsigned int position)
90
{
91
    if (position >= this->count_)
92
    {
93
        return -1;
94
    }
95
 
96
    return (this->bytes_[position / 8] & (0x00000001 << (7 - position)) ? 1 : 0);
97
}
98
 
99
}; // namespace type
100
}; // namespace atom