• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeterCDMcLean / BitLib / 15668442123

15 Jun 2025 11:21PM UTC coverage: 54.204% (+0.7%) from 53.519%
15668442123

Pull #17

github

web-flow
Merge 91a9e717f into 0f0b787a1
Pull Request #17: Truncation policy

10212 of 18774 branches covered (54.39%)

Branch coverage included in aggregate %.

177 of 216 new or added lines in 12 files covered. (81.94%)

214 existing lines in 11 files now uncovered.

6035 of 11200 relevant lines covered (53.88%)

7755103.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.11
/include/bitlib/bit-iterator/bit_value.hpp
1
// =============================== BIT VALUE ================================ //
2
// Project:         The C++ Bit Library
3
// Name:            bit_value.hpp
4
// Description:     A class representing an independent, non-referenced bit
5
// Creator:         Vincent Reverdy
6
// Contributor(s):  Vincent Reverdy [2015-2017]
7
// License:         BSD 3-Clause License
8
// ========================================================================== //
9
#ifndef _BIT_VALUE_HPP_INCLUDED
10
#define _BIT_VALUE_HPP_INCLUDED
11
// ========================================================================== //
12

13

14

15
// ================================ PREAMBLE ================================ //
16
// C++ standard library
17
// Project sources
18
#include "bit_details.hpp"
19
// Third-party libraries
20
// Miscellaneous
21
namespace bit {
22
// ========================================================================== //
23

24

25

26
/* ******************************* BIT VALUE ******************************** */
27
// Bit value class definition
28
class bit_value
29
{
30
    // Friendship
31
    template <class> friend class bit_reference;
32

33
    // Types
34
    public:
35
    static constexpr std::size_t bits = 1;
36
    using size_type = std::size_t;
37

38
    // Lifecycle
39
    public:
40
    constexpr bit_value() noexcept;
41
    template <class T>
42
    constexpr bit_value(bit_reference<T> ref) noexcept;
43
    template <class WordType>
44
    explicit constexpr bit_value(WordType val) noexcept;
45
    explicit constexpr bit_value(bool val) noexcept;
46
    template <class WordType>
47
    constexpr bit_value(WordType val, size_type pos);
48

49
    // Assignment
50
    public:
51
     template <class T>
52
     constexpr bit_value& operator=(bit_reference<T> ref) noexcept;
53
     template <class WordType>
54
     constexpr bit_value& assign(WordType val) noexcept;
55
     template <class WordType>
56
     constexpr bit_value& assign(WordType val, size_type pos);
57

58
     // Bitwise assignment operators
59
    public:
60
    constexpr bit_value& operator&=(bit_value other) noexcept;
61
    constexpr bit_value& operator|=(bit_value other) noexcept;
62
    constexpr bit_value& operator^=(bit_value other) noexcept;
63

64
    // Conversion
65
    public:
66
    explicit constexpr operator bool() const noexcept;
67

68
    // Swap members
69
    public:
70
    void swap(bit_value& other) noexcept;
71
    template <class T>
72
    void swap(bit_reference<T> other) noexcept;
73

74
    // Bit manipulation
75
    public:
76
    constexpr bit_value& set(bool b) noexcept;
77
    constexpr bit_value& set() noexcept;
78
    constexpr bit_value& reset() noexcept;
79
    constexpr bit_value& flip() noexcept;
80

81
    // Implementation details: data members
82
    private:
83
    bool _value;
84

85
    // Bitwise operators
86
    public:
87
    friend constexpr bit_value operator~(
88
        bit_value rhs
89
    ) noexcept;
90
    friend constexpr bit_value operator&(
91
        bit_value lhs,
92
        bit_value rhs
93
    ) noexcept;
94
    friend constexpr bit_value operator|(
95
        bit_value lhs,
96
        bit_value rhs
97
    ) noexcept;
98
    friend constexpr bit_value operator^(
99
        bit_value lhs,
100
        bit_value rhs
101
    ) noexcept;
102

103
    // Comparison operators
104
    public:
105
    friend constexpr bool operator==(
106
        bit_value lhs,
107
        bit_value rhs
108
    ) noexcept;
109
    friend constexpr bool operator!=(
110
        bit_value lhs,
111
        bit_value rhs
112
    ) noexcept;
113
    friend constexpr bool operator<(
114
        bit_value lhs,
115
        bit_value rhs
116
    ) noexcept;
117
    friend constexpr bool operator<=(
118
        bit_value lhs,
119
        bit_value rhs
120
    ) noexcept;
121
    friend constexpr bool operator>(
122
        bit_value lhs,
123
        bit_value rhs
124
    ) noexcept;
125
    friend constexpr bool operator>=(
126
        bit_value lhs,
127
        bit_value rhs
128
    ) noexcept;
129
};
130

131
// Stream functions
132
template <class CharT, class Traits>
133
std::basic_istream<CharT, Traits>& operator>>(
134
    std::basic_istream<CharT, Traits>& is,
135
    bit_value& x
136
);
137
template <class CharT, class Traits>
138
std::basic_ostream<CharT, Traits>& operator<<(
139
    std::basic_ostream<CharT, Traits>& os,
140
    bit_value x
141
);
142

143
// Constants
144
/* ************************************************************************** */
145

146

147

148
// -------------------------- BIT VALUE: LIFECYCLE -------------------------- //
149
// Implicitly default constructs a bit value initialized to zero
150
constexpr bit_value::bit_value(
151
) noexcept
152
: _value(false)
153
{
154
}
155

156
// Implicitly constructs a bit value from a bit reference
157
template <class T>
158
constexpr bit_value::bit_value(
644,915,357✔
159
    bit_reference<T> ref
160
) noexcept
161
: _value(static_cast<bool>(ref))
1,289,830,714✔
162
{
644,915,357✔
163
}
1,289,830,714✔
164

165
// Explicitly constructs a bit from a bool
166
constexpr bit_value::bit_value(
2,708,195✔
167
    bool b
168
) noexcept
2,708,195✔
169
: _value(b)
5,416,390✔
170
{
2,708,195✔
171
}
5,416,390✔
172

173
// Explicitly constructs an aligned bit value
174
template <class WordType>
175
constexpr bit_value::bit_value(
2,709,204✔
176
    WordType val
177
) noexcept
178
: _value(val & 1)
5,418,925✔
179
{
2,709,721✔
180
    static_assert(binary_digits<WordType>::value, "");
2,709,721✔
181
}
5,418,925✔
182

183
// Explicitly constructs an unaligned bit value
184
template <class WordType>
185
constexpr bit_value::bit_value(
186
    WordType val,
187
    size_type pos
188
)
189
: _value((assert(pos < binary_digits<WordType>::value), val >> pos & 1))
190
{
191
    static_assert(binary_digits<WordType>::value, "");
192
}
193
// -------------------------------------------------------------------------- //
194

195

196

197
// ------------------------- BIT VALUE: ASSIGNMENT -------------------------- //
198
// Assigns a bit reference to the bit value
199
template <class T>
200
constexpr bit_value& bit_value::operator=(
201
    bit_reference<T> ref
202
) noexcept
203
{
204
    _value = static_cast<bool>(ref);
205
    return *this;
206
}
207

208
// Assigns the aligned bit of a value to the bit value
209
template <class WordType>
210
constexpr bit_value& bit_value::assign(
211
    WordType val
212
) noexcept
213
{
214
    static_assert(binary_digits<WordType>::value, "");
215
    _value = val & 1;
216
    return *this;
217
}
218

219
// Assigns an unaligned bit of a value to the bit value
220
template <class WordType>
221
constexpr bit_value& bit_value::assign(
222
    WordType val,
223
    size_type pos
224
)
225
{
226
    assert(pos < binary_digits<WordType>::value);
227
    _value = val >> pos & 1;
228
    return *this;
229
}
230
// -------------------------------------------------------------------------- //
231

232

233

234
// ---------------- BIT VALUE: BITWISE ASSIGNMENT OPERATORS ----------------- //
235
// Assigns the value of the bit through a bitwise and operation
236
constexpr bit_value& bit_value::operator&=(
237
    bit_value other
238
) noexcept
UNCOV
239
{
UNCOV
240
    _value &= other._value;
UNCOV
241
    return *this;
UNCOV
242
}
243

244
// Assigns the value of the bit through a bitwise or operation
245
constexpr bit_value& bit_value::operator|=(
246
    bit_value other
247
) noexcept
UNCOV
248
{
UNCOV
249
    _value |= other._value;
UNCOV
250
    return *this;
UNCOV
251
}
252

253
// Assigns the value of the bit through a bitwise xor operation
254
constexpr bit_value& bit_value::operator^=(
255
    bit_value other
256
) noexcept
UNCOV
257
{
UNCOV
258
    _value ^= other._value;
UNCOV
259
    return *this;
UNCOV
260
}
261
// -------------------------------------------------------------------------- //
262

263

264

265
// ------------------------- BIT VALUE: CONVERSION -------------------------- //
266
// Explicitly converts the bit value to a boolean value
267
constexpr bit_value::operator bool(
33,293,563✔
268
) const noexcept
269
{
33,294,080✔
270
    return _value;
66,587,643✔
271
}
33,294,080✔
272
// -------------------------------------------------------------------------- //
273

274

275

276
// ------------------------ BIT VALUE: SWAP MEMBERS ------------------------- //
277
// Swaps the bit value with another bit value
278
inline void bit_value::swap(
279
    bit_value& other
280
) noexcept
UNCOV
281
{
UNCOV
282
    std::swap(*this, other);
UNCOV
283
}
284

285
// Swaps the bit value with the value of a bit reference
286
template <class T>
287
void bit_value::swap(
288
    bit_reference<T> other
289
) noexcept
290
{
291
    if (other != _value) {
292
        flip();
293
        other.flip();
294
    }
295
}
296
// -------------------------------------------------------------------------- //
297

298

299

300
// ---------------------- BIT VALUE: BIT MANIPULATION ----------------------- //
301
// Sets the value of the bit to the provided boolean value
302
constexpr bit_value& bit_value::set(
303
    bool b
304
) noexcept
UNCOV
305
{
UNCOV
306
    _value = b;
UNCOV
307
    return *this;
UNCOV
308
}
309

310
// Sets the value of the bit to 1
311
constexpr bit_value& bit_value::set(
312
) noexcept
UNCOV
313
{
UNCOV
314
    _value = true;
UNCOV
315
    return *this;
UNCOV
316
}
317

318
// Resets the value of the bit to 0
319
constexpr bit_value& bit_value::reset(
320
) noexcept
UNCOV
321
{
UNCOV
322
    _value = false;
UNCOV
323
    return *this;
UNCOV
324
}
325

326
// Flips the value of the bit
327
constexpr bit_value& bit_value::flip(
328
) noexcept
UNCOV
329
{
UNCOV
330
    _value = !_value;
UNCOV
331
    return *this;
UNCOV
332
}
333
// -------------------------------------------------------------------------- //
334

335

336

337
// ---------------------- BIT VALUE: BITWISE OPERATORS ---------------------- //
338
// Returns the result of a bitwise not on the right hand side
339
constexpr bit_value operator~(
2,708,984✔
340
    bit_value rhs
341
) noexcept
342
{
2,708,984✔
343
    using type = unsigned int;
2,708,984✔
344
    return bit_value(static_cast<type>(!rhs._value));
5,417,968✔
345
}
2,708,984✔
346

347
// Returns the result of a bitwise and between the left and right hand sides
348
constexpr bit_value operator&(
349
    bit_value lhs,
350
    bit_value rhs
351
) noexcept
UNCOV
352
{
UNCOV
353
    using type = unsigned int;
UNCOV
354
    return bit_value(static_cast<type>(lhs._value & rhs._value));
UNCOV
355
}
356

357
// Returns the result of a bitwise or between the left and right hand sides
358
constexpr bit_value operator|(
359
    bit_value lhs,
360
    bit_value rhs
361
) noexcept
UNCOV
362
{
UNCOV
363
    using type = unsigned int;
UNCOV
364
    return bit_value(static_cast<type>(lhs._value | rhs._value));
UNCOV
365
}
366

367
// Returns the result of a bitwise xor between the left and right hand sides
368
constexpr bit_value operator^(
369
    bit_value lhs,
370
    bit_value rhs
371
) noexcept
UNCOV
372
{
UNCOV
373
    using type = unsigned int;
UNCOV
374
    return bit_value(static_cast<type>(lhs._value ^ rhs._value));
UNCOV
375
}
376
// -------------------------------------------------------------------------- //
377

378

379

380
// -------------------- BIT VALUE: COMPARISON OPERATORS --------------------- //
381
// Checks if the left hand side is equal to the right hand side
382
constexpr bool operator==(
637,258,870✔
383
    bit_value lhs,
384
    bit_value rhs
385
) noexcept
386
{
637,260,774✔
387
    return lhs._value == rhs._value;
1,274,519,644✔
388
}
637,260,774✔
389

390
// Checks if the left hand side is non equal to the right hand side
391
constexpr bool operator!=(
1,016✔
392
    bit_value lhs,
393
    bit_value rhs
394
) noexcept
395
{
1,016✔
396
    return lhs._value != rhs._value;
2,032✔
397
}
1,016✔
398

399
// Checks if the left hand side is less than the right hand side
400
constexpr bool operator<(
401
    bit_value lhs,
402
    bit_value rhs
403
) noexcept
UNCOV
404
{
UNCOV
405
    return lhs._value < rhs._value;
UNCOV
406
}
407

408
// Checks if the left hand side is less than or equal to the right hand side
409
constexpr bool operator<=(
410
    bit_value lhs,
411
    bit_value rhs
412
) noexcept
UNCOV
413
{
UNCOV
414
    return lhs._value <= rhs._value;
UNCOV
415
}
416

417
// Checks if the left hand side is greater than the right hand side
418
constexpr bool operator>(
419
    bit_value lhs,
420
    bit_value rhs
421
) noexcept
UNCOV
422
{
UNCOV
423
    return lhs._value > rhs._value;
UNCOV
424
}
425

426
// Checks if the left hand side is greater than or equal to the right hand side
427
constexpr bool operator>=(
428
    bit_value lhs,
429
    bit_value rhs
430
) noexcept
UNCOV
431
{
UNCOV
432
    return lhs._value >= rhs._value;
UNCOV
433
}
434
// -------------------------------------------------------------------------- //
435

436

437

438
// ---------------------- BIT VALUE: STREAM FUNCTIONS ----------------------- //
439
// Extracts a bit value from an input stream
440
template <class CharT, class Traits>
441
std::basic_istream<CharT, Traits>& operator>>(
442
    std::basic_istream<CharT, Traits>& is,
443
    bit_value& x
444
)
445
{
446
    using stream_type = std::basic_istream<CharT, Traits>;
447
    using traits_type = typename stream_type::traits_type;
448
    using ios_base = typename stream_type::ios_base;
449
    constexpr char zero = '0';
450
    constexpr char one = '1';
451
    constexpr typename stream_type::int_type eof = traits_type::eof();
452
    typename ios_base::iostate state = ios_base::goodbit;
453
    typename stream_type::char_type char_value = 0;
454
    typename stream_type::int_type int_value = 0;
455
    typename stream_type::sentry sentry(is);
456
    bool ok = false;
457
    bit_value tmp = x;
458
    if (sentry) {
459
        try {
460
            int_value = is.rdbuf()->sbumpc();
461
            if (traits_type::eq_int_type(int_value, eof)) {
462
                state |= ios_base::eofbit;
463
            } else {
464
                char_value = traits_type::to_char_type(int_value);
465
                if (traits_type::eq(char_value, is.widen(zero))) {
466
                    tmp.reset();
467
                    ok = true;
468
                } else if (traits_type::eq(char_value, is.widen(one))) {
469
                    tmp.set();
470
                    ok = true;
471
                } else {
472
                    int_value = is.rdbuf()->sputbackc(char_value);
473
                    if (traits_type::eq_int_type(int_value, eof)) {
474
                        state |= ios_base::failbit;
475
                    }
476
                }
477
            }
478
        } catch(...) {
479
            is.setstate(ios_base::badbit);
480
        }
481
    }
482
    if (ok) {
483
        x = tmp;
484
    } else {
485
        state |= ios_base::failbit;
486
    }
487
    state ? is.setstate(state) : void();
488
    return is;
489
}
490

491
// Inserts a bit value in an output stream
492
template <class CharT, class Traits>
493
std::basic_ostream<CharT, Traits>& operator<<(
494
    std::basic_ostream<CharT, Traits>& os,
495
    bit_value x
496
)
UNCOV
497
{
498
    constexpr char zero = '0';
×
499
    constexpr char one = '1';
×
500
    return os << os.widen(x ? one : zero);
×
UNCOV
501
}
502
// -------------------------------------------------------------------------- //
503

504

505

506
// -------------------------- BIT VALUE: CONSTANTS -------------------------- //
507
// Constant bit values
508
constexpr bit_value bit_off(0U);
509
constexpr bit_value bit_on(1U);
510
constexpr bit_value bit0(0U);
511
constexpr bit_value bit1(1U);
512
// -------------------------------------------------------------------------- //
513

514

515

516
// ========================================================================== //
517
} // namespace bit
518
#endif // _BIT_VALUE_HPP_INCLUDED
519
// ========================================================================== //
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc