-4

I want to generate a 16 bit unsigned integer (uint16_t) which could represent following:

  1. First 2 digits representing some version like 1, 2, 3 etc.
  2. Next 3 digits representing another number may be 123, 345, 071 etc.
  3. And last 11 digits representing a number T234, T566 etc.

How can we do this using objective C. I would like to parse this data later on to get these components back. Please advise.

2
  • By "digits" do you mean "bits"? Two bits gives you a range of 0-3 while three bits gives you a range of 0-7. So there is no way to store values such as 123 or 071 in 3 bits.
    – rmaddy
    Commented Jul 22, 2013 at 23:22
  • Objective-C allows bitwise operators just like C. Commented Jul 22, 2013 at 23:27

1 Answer 1

5

I think you are misunderstanding just what uint16_t means. It doesn't mean a 16 digit decimal number (which would be any number between 0 and 9,999,999,999,999,999). It means an unsigned number that can be expressed using 16 bits. The range of such a value is 0 to 65535 in decimal. If you really wanted to store the numbers you are talking about you would need 52 bits. You would also be making things very difficult for yourself, since you wouldn't easily be able to extract the first two decimal digits from that 52 bit sequence; You'd have to treat the number as a decimal value then modulus 100 it, you couldn't just say it's bits 1 to 8.

There is a scheme called Binary Coded Decimal that could help you. You would take a 64 bit value (uint64_t) and you'd say that within this value the bits 1-7 are the version (which could be a value up to 127), bits 8-17 are the second number (which could be a value up to 1023) and bits 18-63 could be your third number (those 46 bits would be able to store a number up to 70,368,744,177,663.

All this is technically possible, but you are really going to be making things hard for yourself. It looks like you are storing a version, minor version and build number and most people do that using strings, not decimals

1
  • Using DPD you can pack 18 decimal digits into a 64-bit number
    – phuclv
    Commented May 25, 2014 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.