Re: Setting value of a class

From: JKop (NULL_at_NULL.NULL)
Date: 06/20/04


Date: Sun, 20 Jun 2004 14:39:47 GMT

Rolf Magnus posted:

> JKop wrote:
>
>> Consider the following platform:
>>
>> char = 8-bit
>>
>> short = 16-bit
>>
>> int = 32-bit
>>
>> long = 32-bit
>>
>>
>> The largest value possible for unsigned long is 4,294,967,295 = 4.29
>> billion approx.
>>
>> Now imagine you're some sort of census people and you're keeping track
>> of the population of the earth. You're going to want a 33-Bit Number.
>> You look around and you find a library that has a certain class that
>> gives you a 64- Bit number.
>> The following is heavily contrived... but just bare with me. So
>> here's
>> how the system will work.
>>
>> #include <superint.h>
>>
>> class Human
>> {
>> protected:
>>
>> static SixtyFourBitInt amount_humans = 0;
>>
>> public:
>> Human(void)
>> {
>> ++amount_humans; }
>>
>> ~Human(void)
>> {
>> --amount_humans; } };
>>
>> static SixtyFourBitInt Human::amount_humans;
>>
>>
>> This should all be fine and dandy... but how would one, or how does
>> one (to those who have experience with this) actually manually set its
>> value? Consider the following:
>>
>>
>> SixtyFourBitInt numbr;
>>
>> numbr = 5000000000;
>>
>> This won't compile obviously.
>>
>>
>>
>> Do you have to do something like the following?:
>>
>>
>> SixtFourBitInt numbr;
>>
>> numbr = 4000000000;
>>
>> numbr += 1000000000;
>>
>>
>> How exactly would you do it?
>
> That depends on the class you use. One way around (that is e.g. used in
> std::bitset) is to use a string:
>
> SixtyFourBitInt numbr("5000000000");

Yes, this works. But, I think it's dirty. Firstly, it's going into the realm
of even representing a number using digits, and in so-doing, specifiying a
radix and set of digits, ie. the decimal system and the digits, '0' through
'9'.

>
> Or it could be split up into two 32 bit parts, which would then best be
> specified as hex values:
>
> SixtyFourBitInt numbr(0x1, 0x2A05F200);

...but is this defined behaviour?? I myself may think of the number 25 being
represented in binary as:

0001 1001

But that doesn't mean the computer does!! I may store it backwards:

1001 1000

or topsy-turvy:

0011 1000

>
>> Another example, consider another class:
>>
>>
>> class InternationalPhoneNumber;
>>
>>
>> This class is going to (well..., wants to) use 4 bits for each digit,
>> 0 through 9. Obviously, at some stage, the programmer will want to set
>> the value of this beast. Would the following be the best course of
>> action?:
>>
>> InternationalPhoneNumber disneylands_number = "44990561002";
>>
>> And then just manipulate the string into 4 bits for each digit?
>
> I would use a string directly to store a phone number. This will make it
> possible to support special values like # and *, as well as separators
> like / and -. Also, you won't know how long the number will be. It
> might have 20 digits or 30 or even 40.

I want to explicitly limit an international phone number to being comprised
of just the digits 0 through 9.
            Another thing, I want to conserve memory by using 4 bits as opposed to
the possible 8 bits used for a char.

And even with the 4 bits, I still have 6 combinations left over for the #
symbol or whatever.

Thanks for the input. Keep the suggestions coming!

-JKop



Relevant Pages

  • Re: bigint queston C++ classes
    ... I suggest an array of unsigned char, or unsigned short, or perhaps unsigned ... You'll need ceil/log) base-b digits to store x, ... these are not strings, ...
    (comp.programming)
  • Re: bigint queston C++ classes
    ... I suggest an array of unsigned char, or unsigned short, or perhaps unsigned ... routine for turning the end result into base ten for display, ... You'll need ceil/log) base-b digits to store x, ...
    (comp.programming)
  • Re: Fibonacci numbers
    ... | Of course there is the question of how to store the number. ... Instead of using characters to represent decimal digits, ... It is easier than dealing with characters, as you don't need to offset ... the way that we write numbers in strings. ...
    (comp.lang.cpp)
  • large number, store and sort
    ... database and it can store with or without spaces (I am ... presently planning to store it without spaces). ... to be sorted on the first seven of the last eight digits ... What field type would be best to use - text, ...
    (microsoft.public.access.tablesdbdesign)
  • Re: large number, store and sort
    ... >of digits rather than a number: ... >If you need to sort on just some of the digits it would ... >digits) means any leading zeroes are automatically ... >>presently planning to store it without spaces). ...
    (microsoft.public.access.tablesdbdesign)