Skip to main content
14 votes
Accepted

Bit array implementation

Don't write a function to return a constant You don't need uint_size(), as the returned value never changes (for a given target platform). You can replace it with ...
Toby Speight's user avatar
  • 81.7k
10 votes

Bitmasking and searching consecutive 1's

<bits/stdc++.h> (like everything in your compiler's bits/ subtree) is not a standard header and therefore not portable. ...
Toby Speight's user avatar
  • 81.7k
10 votes

FlagSet with C++ templates

IndexOf can probably be a nested type of FlagSet, unless you intend for that to be part of the public interface. ...
indi's user avatar
  • 13.7k
10 votes

Huffman coding, using vector<bool>

std::vector<bool> I wrote the code assuming vector represents each bool with a single bit. Is there a standard way to check whether the STL used by the ...
user555045's user avatar
  • 10.7k
8 votes

Bit array implementation

I have a number of comments, so I'll just sort of spray them out. Using a proxy reference type is tricky, and can lead to some interesting problems. For example, a class that provides proxy ...
ruds's user avatar
  • 3,321
8 votes
Accepted

Counting the number of 1 bits in a sorted bit array

I agree with your assessment of the code being unnecessarily long and awkward. It looks like the root cause is an opportunistic optimization. The bad news is that most of the secondary tests (like <...
vnp's user avatar
  • 57.3k
8 votes

C++ data type to store and manipulate individual bits

not directly answering your question, but just a notice: I would say the more elegant way would be just extending the std::bitset and add the extra methods you need,...
StPiere's user avatar
  • 229
8 votes

A Dynamic Bitset that I wrote and will use in my compression algortithm. This will be used to store the chars as bits from fstream

Advice 1 - std::size_t for indexing and counting Instead of the following: unsigned int arrayLength; unsigned int bitLength; I ...
coderodde's user avatar
  • 29.8k
7 votes

Counting the number of 1 bits in a sorted bit array

This removes the learning part of this (if your goal is to try to learn to implement a binary search in this context), but I contend the most pythonic solution would be to use ...
Bailey Parker's user avatar
7 votes
Accepted

C++ data type to store and manipulate individual bits

Here are some things that may help you improve your program. Use all required #includes The templated class uses CHAR_BIT but ...
Edward's user avatar
  • 66.6k
7 votes
Accepted

Type-safe flag sets (bit fields) that make sense

You're not the first to be frustrated at the bitmasks we've inherited from K&R - it would be really nice if the language provided better support in this area, and many libraries have given us ...
Toby Speight's user avatar
  • 81.7k
6 votes
Accepted

Bitmask adapter

The test for enable_if_all_same_as_T seems a bit over-restrictive - surely all the arguments only need to be assignable to ...
Toby Speight's user avatar
  • 81.7k
6 votes

Bitmasking and searching consecutive 1's

When you post code to be reviewed: If your code asks for input, provide a set of valid and invalid inputs. If your code output a result, show the expected output. Since you present your code, it may ...
Calak's user avatar
  • 2,371
6 votes
Accepted

A Dynamic Bitset that I wrote and will use in my compression algortithm. This will be used to store the chars as bits from fstream

Use namespaces DynamicBitset might be a sufficiently unique name, but if you put it inside a namespace, you're much less likely to collide with some other class ...
Cris Luengo's user avatar
  • 6,237
5 votes

How to make my “custom integer type” perform better?

A significant performance drain is bit-by-bit computation loops such as ...
user555045's user avatar
  • 10.7k
5 votes

Bitmasking and searching consecutive 1's

Don't declare multiple variables on a single line. it is error prone and more difficult to read. int n,q,k,count; should be: ...
Summer's user avatar
  • 2,330
5 votes

Macros for bitsets / bit-flags in C

Most severe issue: Never invent secret macro languages! This is about the single-worst thing a C programmer can ever do, all categories. You are perfectly free to assume that any C programmer will ...
Lundin's user avatar
  • 4,794
5 votes
Accepted

Macros for bitsets / bit-flags in C

Small portability bug: if we're using false in the macros, then bflags.h should include ...
Toby Speight's user avatar
  • 81.7k
5 votes

Optimize memory and disk usage for sequences of n-bit values

Design review Your requirements are a bit vague; or rather, a bit contradictory. You say you want maximum memory efficiency (in the first paragraph)… but then you say you don’t, really (in the first ...
indi's user avatar
  • 13.7k
4 votes

Bit string builder for Java

It looks like you want to optimize something but you did not define what are your performance requirements. Your tests do not justify your implementation. Why the state of builder is stored in array ...
Karol's user avatar
  • 71
4 votes
Accepted

FlagSet with C++ templates

It's a great idea to provide a type-safe bit-mask, I like your idea a lot. That said, I feel like your code is in great need of modernization and simplification. Recursion through inheritance is now ...
papagaga's user avatar
  • 5,767
4 votes

Emulating Virtual Registers Part 3

Here are some thoughts on how to further improve your program. Use all required #includes The program refers to CHAR_BIT which ...
Edward's user avatar
  • 66.6k
4 votes

Individual bit access in C

This code is not portable. The order of bit-fields within a word is completely compiler-dependent, so the test that appears to work on one platform may give completely different results on another. ...
Toby Speight's user avatar
  • 81.7k
4 votes

Huffman coding, using vector<bool>

Missing I/O error checking Your code does not check whether reading from or writing to files has succeeded, with the worst possible outcome being silent data corruption. The proper way to check for ...
G. Sliepen's user avatar
  • 61.7k
4 votes

Optimize memory and disk usage for sequences of n-bit values

Consider using a proper compression algorithm Your method saves 25% of storage space, and is relatively simple to implement, but if the goal is to reduce memory and disk usage as much as possible, you ...
G. Sliepen's user avatar
  • 61.7k
4 votes

Optimize memory and disk usage for sequences of n-bit values

You're missing the header includes to provide std::array, std::pair, std::addressof(), ...
Toby Speight's user avatar
  • 81.7k
4 votes

A Dynamic Bitset that I wrote and will use in my compression algortithm. This will be used to store the chars as bits from fstream

operator++ increments the whole thing as a BigInt? It's incredibly inefficient to do this one bit at a time; you're running on a CPU with ALUs that can propagate ...
Peter Cordes's user avatar
  • 3,416
3 votes
Accepted

Program containing several bitset based classes including chars, strings, ints and a base bitset class

I'm not sure how to review your code, because I don't understand what you're aiming for. There is a std::bitset class but it's different from yours: its size is ...
papagaga's user avatar
  • 5,767
3 votes

Mixed-width operations for std::bitset

One aspect that could surprise users is that operator & always narrows types to match. That's a departure from the usual guidance of "do what the integers do". ...
Toby Speight's user avatar
  • 81.7k
3 votes
Accepted

Creating and storing binary arrays

I see a number of things that may help you improve your program. Understand const In a number of places in the code we have function declarations like this: <...
Edward's user avatar
  • 66.6k

Only top scored, non community-wiki answers of a minimum length are eligible