Byte * Byte = Short Int, How Byte*Byte*.... = Float ?

Feb 9, 2012 at 4:08pm
Just a question, if I intend people can only get level 255 on some game,if they make it at level 256.I would create dynamically a byte and multiply with the first to get an short int.

But how can I do it with float ?
Feb 9, 2012 at 4:14pm
closed account (S6k9GNh0)
This isn't really a C++ question but...

If you intend to have people reach a maximum of level 255, then how are they supposed to go past 255?

A float is no different from a integer in this case anyways. "myFloat = myByte1 * myByte2" is fine. You might need to cast one of the integers to a float type which doesn't cost anything and is probably safe cast statically.
Feb 9, 2012 at 4:47pm
Byte is char anyway,I'd use it to have 255.There is no byte available.

I want to have an optimized data handling,which only creates more data if need to.

Firstly, a char for level is good,if they need more,the system would create them.

But a float have it integer part and the float part.

I suppose I should convert into 2 parts and add them.
Feb 9, 2012 at 4:59pm
closed account (S6k9GNh0)
I'm not really sure I understand. What's the purpose of the float?

In addition, you'll find that allocating enough data to allow for the maximum amount of levels is more efficient than trying to allocate memory dynamically when it's needed, especially in cases like this. You're better off just using a short or long.
Last edited on Feb 10, 2012 at 2:09pm
Feb 9, 2012 at 6:53pm
I think I understand what OP is asking.
If you initially have a single byte A to store a number and you need to increase the capacity, you can do this by adding another byte B. Then the actual number is B*256+A. This is called "fixed point arithmetic" because each bit or group of bits represents a specific magnitude (A's magnitude is 1 and B's is 256). It's not possible to do something analog to this for float because float uses floating point arithmetic; that is, the bits represent a variable magnitude depending on an exponent.
Last edited on Feb 9, 2012 at 6:55pm
Feb 9, 2012 at 6:58pm
Maybe I'm missing something here. If you dont' want a maximum of 255, then why use a byte? Why not just use a full int?
Feb 10, 2012 at 2:57am
Disch is right.

unsigned int A;
unsigned char B;
unsigned char C;

Cast the calculation of B and C to an integer.

A = (int)(B * C);

Straight forward. The compiler take care of it.
Feb 10, 2012 at 4:11am
Why is everyone so dead set on multiplying unsigned characters together? I'm with Disch, use an unsigned int. There is no need to do any math or anything like that, you're just wasting time and effort for something that is inbuilt.
Feb 10, 2012 at 2:39pm
I should also note that using smaller data types is not necessarily "more optimized". It might actually be faster to use an int than it would be to use a char for a variable. Especially if you're doing what you're doing and casting to/from larger types whenever your add/multiply/whatever.

Also, depending on the context of how the char is used, it might not even use any less memory than an int (the char might be padded to the next 4-byte boundary, for example).


Really, don't worry about this kind of micro-optimizing crap. Even if my some chance it does make some kind of performance improvement, it would be so small that nobody would ever notice.

Use types that are large enough for what you want.
Feb 10, 2012 at 3:26pm
Disch You are correct. Depending of what type of controller used 8, 16, 32 or 64bit buswidth in some cases 128bit will depend on speed and optimize the code will be. In a regular PC always use int (long int) which take use of the structure of the controller. Why ever use char???? Char's are for high speed 8bit microcontroller. Forgot to say. Most compilers for C++ in the PC world turn int's into long int (32bits) to take advance of the computer structure.
Last edited on Feb 10, 2012 at 3:29pm
Topic archived. No new replies allowed.