0

I'm here today to ask you some wise hints on how I can handle big std::bitset the right way. Specifically, I have to manage bitset as large as 256 bits (even more eventually), and until I need to perform classical binary operations, I'm not in trouble. The problem arise when I need to perform subtraction between bitsets. Usually, I would translate them with to_ulong() or to_ullong(), performing subtraction and re-translate to bitset, but with n_bits > 64bit things are not easy as before.

So, I'm here to ask you if there's a way to perform subtraction between bitset in another way. I know that it also exist one (or maybe more?) BigNum C++ library, but I am looking for something that could be done without adding external libs. I see the usage of lib as the last possible way to fix this problem.

Thank you!

panc_fab
  • 522
  • 1
  • 7
  • 24
  • 1
    Since subtraction is just adding a negative number, don't you just need this: https://stackoverflow.com/questions/13282825/adding-binary-numbers-in-c – NathanOliver Feb 23 '21 at 15:46
  • Googling std::bitset math found two promising links: https://flylib.com/books/en/2.131.1/performing_arithmetic_on_bitsets.html and https://alikhuram.wordpress.com/2013/05/15/performing-arithmetic-on-bitsets-in-c/ – Dean Johnson Feb 23 '21 at 15:57
  • A `bitset` manages bits. If you need integer operations, use an integer type. – Pete Becker Feb 23 '21 at 17:21
  • @PeteBecker No, I need binary subtraction, as explained. I don't need integer operation. I described the way I usually overcome the problem for small bitset (ie smaller than 64bit). – panc_fab Feb 24 '21 at 08:36
  • @DeanJohnson they're perfect! I've googled a lot yesterday without success, I was looking specifically to "bitset subtraction in c++" but only gave me couple results of no interest. thank you a lot! – panc_fab Feb 24 '21 at 09:28
  • 1
    "Binary subtraction" is not a thing. What you described, converting to an integer, subtracting, and converting back, is integer subtraction. When you mentioned "binary subtraction" my first guess was "remove1-bits from one of the values that correspond to 1-bits in the other value". So 110 minus 011 is 100. That's a fairly common operation, and when you have sets, it's called subtraction. With bits it's A & ~B. – Pete Becker Feb 24 '21 at 13:02
  • @PeteBecker Oh, well I trust you :) I always get confused when it comes to bits, I don't use them quite often. Thank you for the explanation! – panc_fab Feb 24 '21 at 13:39
  • 1
    To clarify somewhat, every value in a program is represented by bits. Usually we treat an aggregation of those bits as some larger thing, such as an unsigned integer, where we talk about the range of values that it can represent. We can also look at the individual bits in that unsigned integer, and that's where things can easily get muddled. We often represent "bits" as integer values: `1u << 0`, `1u << 1`, `1u << 2`, etc. This lets us store multiple "bits" in an integer value. We can manipulate those bit values with bitwise operators `&`, `|`, `^`, and `~`. We can also treat ... – Pete Becker Feb 24 '21 at 13:45
  • 1
    ... that aggregation of bits as an integer value, because that's how we represent those bit values. That lets us use arithmetic operations on that aggregation of bits. Or, to go the other way around, we can use bit manipulations to modify integer values. For example, `x = x & ~1` removes the low bit from the value in `x`; from an integer perspective, it converts the value of `x` into an even number. `std::bitset` is intended to emphasize bit-ness rather than integer-ness. For convenience, you can assign integer values to a bitset, and you can convert a bitset to an integer value. – Pete Becker Feb 24 '21 at 13:49

0 Answers0