445,742 Members | 1,044 Online Need help? Post your question and get tips & solutions from a community of 445,742 IT Pros & Developers. It's quick & easy.

# Setting value of a class

 P: n/a 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 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? 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? Enlighten me! -JKop Jul 22 '05 #1
3 Replies

 P: n/a 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 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"); Or it could be split up into two 32 bit parts, which would then best be specified as hex values: SixtyFourBitInt numbr(0x1, 0x2A05F200); 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. Jul 22 '05 #2

 P: n/a 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 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 Jul 22 '05 #3

 P: n/a On Sun, 20 Jun 2004 12:40:39 GMT, JKop wrote in comp.lang.c++: 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 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? 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? Enlighten me! -JKop Are there actual conforming C++ compilers today in 32-bit hosted environments that do not provide a 64-bit integer type ("long long" or __int64)? Even if there is one and you need to program for it, using binary would be much, much simpler. The data members of such a class would both be 32 bits, one for the low word and one for the high word. Coding addition, subtraction, and multiplication for such a class is almost trivial in binary, although division is more complicated in either binary or BCD. Once you have the addition and multiplication operations working, parsing a value from a text string is truly trivial, algorithm with overflow check omitted: result = 0 while another digit result *= 10 result += digit - '0' end while return result -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html Jul 22 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 