0

I have to write a class that has a natural number taking values only in the (0, 15] range.

I can easily do some checking logic in the setter/constructor for that field - but is there any pre-defined data type which can achieve this behavior?

  • No. In particular, all predefined numeric types include the neutral element of ordinary addition, which your proposed type does not. – EOF Oct 22 '21 at 20:07
  • You could use a nibble, but I think any other reasonable approach would be better. – user888379 Oct 22 '21 at 20:10
  • @user888379 No, a nibble would naturally represent the values [0 .. 16), not (0 .. 15]. – EOF Oct 22 '21 at 20:13
  • @EOF I should have looked more closely at the endpoints of the range before making my unhelpful suggestion. And we're also ignoring the signed vs unsigned issue... – user888379 Oct 22 '21 at 20:34
  • @user888379 What "signed vs unsigned issue"? There is no such issue here. – EOF Oct 22 '21 at 20:47
  • I was just positing that if we treated the high order bit of the nibble as a sign bit, then the range would be way out of whack. Just being silly, and I'm not actually helping the OP here, so I'll refrain from further silliness. – user888379 Oct 22 '21 at 22:41

0 Answers0