# Getting integers to hold zeros

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-25-2007
Wolves
Getting integers to hold zeros
Hey all, I'm sure this is a stupidly easy question, but it must be asked:

How would one go about getting integers to hold zeros before any other number?
Like, to get an integer to hold the value 001 and not just 1.

I know it's probably been answered before, but it's such a simple question that I think it'd
be more work searching for the answer than asking the question again.
• 05-25-2007
laserlight
That cannot be done. What can be done, however, is to format the number such that it is padded with the required number of zeros in an output stream or a stringstream.
• 05-25-2007
Dave_Sinkula
Quote:

Originally Posted by Wolves
Hey all, I'm sure this is a stupidly easy question, but it must be asked:

How would one go about getting integers to hold zeros before any other number?
Like, to get an integer to hold the value 001 and not just 1.

I know it's probably been answered before, but it's such a simple question that I think it'd
be more work searching for the answer than asking the question again.

The real question that you are attempting to ask regards (possibly) the underlying storage of a value and the representation of a value in text with regard to a certain base.

One is one is one. Looking at it differently never changes the value, only the representation. (Although a larger number would be more helpful in this awful analogy.)
• 05-26-2007
simpleid
I might have an idea for what you want. With some creativity and a bit mask or two it will work.

First, pick the number that you want, say...

int value = 255;

then, say you want three 0's before 255, to be 000255, what we have to do is, change the value to store 1000255.

then use a bitmask to only retrieve the 000255 portion, you could programmatically search for the first 1, and through some logic count the 0's, then format the output correctly.

Does that sound close to what you want? Oh and laserlight, anything can be done. ;-)
• 05-26-2007
Dave_Sinkula
Quote:

Originally Posted by simpleid
then, say you want three 0's before 255, to be 000255, what we have to do is, change the value to store 1000255.

Chalk up two that need to understand value vs text representation.
• 05-26-2007
simpleid
dave, i'm not sure what you're trying to say but i think you're making a sarcastic remark at me so allow me to point out that the 'representation' doesn't matter, the effect is created which is what was asked for. who cares if he thinks 001 + 1 = 2 is different from 1+1=2, if he wants his output to be 00x then so be it, it's possible.

I do understand the difference, but that doesn't mean I can't create any hypothetical effect I please regardless.

Or maybe you're vision is too acute to see the greater good here, to let individuals do as they please. how ever meaningless something is -to you- means nothing, if someone has a purpose then to help them achieve that is respectful.

Chalk up one simpleton that doesn't 'get it', seems some people around here prefer to argue their way out of providing creative solutions in a clouded argument citing 'irrationality.'
• 05-26-2007
Dave_Sinkula
Quote:

Originally Posted by simpleid
dave, i'm not sure what you're trying to say but i think you're making a sarcastic remark at me so allow me to point out that the 'representation' doesn't matter

Well, values don't have "leading zeros". Or any value does. So 255 or 1000255 both do or both don't. As a value.

As a text representation, there is a difference. But you don't bit mask a text representation, so your solution seems ambiguous.

And then there is the idea of a bit mask for 1000000. It would seem more suited to the % operator. But again, that would be value-land.

But if it were the case that you added a million and then "modded" by a million, then you'd be left with a value of 255. Which of course suffers the initial problem; recurse indefinitely.

So in regard to the OP's question: it's a non-question. Use printf specifiers.
• 05-26-2007
simpleid
say you create a game which keeps track of a score and you want it to have a limit. you want to display the score as 00001, kill another guy, 00002, maybe 99999 is the limit.

and lets say for the hell of it, you want to track the zeros in a single int. if for nothing more than the intellectual curiosity to see it done.

well i'll come up with the code later tonight after i close up a project, i'll post it soon. my motivation is nothing more than our discourse.

>:-)
• 05-26-2007
Dave_Sinkula
Dandy. Along the way, make mental notes of "representation" and "value".

Keep in mind that 01 (octal 1), 0x1 (hexadecimal 1), 1 (decimal 1) are all the same value; they are different representations.
• 05-26-2007
simpleid
i don't know what point you're making. if he's asking for something and you're just stating facts which don't stop an individual from giving a solution, then what are you saying?

You might want to consider that there's a difference in how something is shown and what it means. Aesthetics might be an angle you're not considering?

It sounds like what you're doing is making a point which doesn't contribute to the issue he's having. so were you really just being sarcastic?

if i ask someone to tell me how the toaster works, and someone tells me about the way butter looks on the bread while in the toaster, then that doesn't really say anything about how to continue does it?

• 05-26-2007
Dave_Sinkula
Quote:

Originally Posted by Wolves
Like, to get an integer to hold the value 001 and not just 1.

The rest is smoke and mirrors.

Quote:

Originally Posted by simpleid
i don't know what point you're making. if he's asking for something and you're just stating facts which don't stop an individual from giving a solution, then what are you saying?

You've missed the previously presented solutions?
Quote:

Originally Posted by laserlight
That cannot be done. What can be done, however, is to format the number such that it is padded with the required number of zeros in an output stream or a stringstream.

Quote:

Originally Posted by Dave_Sinkula
Use printf specifiers.

Quote:

What is yours? Disinformation?
• 05-26-2007
simpleid
Actually, what i've planned out and am working on so far seems to be working.

I'm using a single bit as an indicator of where leading zeros begin then find the next bit where the value will be extracted. the program keeps track of the limit the single integer can take, designated by the number of places that can be used. this seems to be providing some extra functionality, and it's all in a single number.

doing this with setValue (int va, int number_places), then retrieve with getValue (int val)

1. if num_digits = 5 in setValue then there will be 5 places for values. ( 00000 )
2. if 200 is passed to setValue then we will have 00200 returned.
3. showValue will return 00200 without knowing num_digits.

is that the "disinformation" you're referring to?

see this way the leading zeros are not always the same, it's actually based on how many values there is allowed.
• 05-26-2007
Dave_Sinkula
All righty, you win. You're reinventing printf. Manipulate a representation and not a value; just what I said. I bow before you.
• 05-26-2007
simpleid
well, i didn't really realize printf did it that way exactly, i just assumed there wasn't an obvious answer since he asked. and i tend to think 'do from scratch' before i think 'does it exist.' /faulty. i know i was mistaken. this was enlightening anyway.
• 05-27-2007
MWAAAHAAA
BLAH BLAH BLAH:

printf("&#37;03i\n", 1);

As laserlight already put it, storing a value in memory, or representing it on your screen are two different things. Just imagine using the number one, i.e., 1, in some arbitrary mathematical operation. Does it matter whether you _write_ it as 001, 0000001, or just plain and simple 1? Of course not.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last