Character arrays

Started by
11 comments, last by kingy 22 years, 8 months ago
LOL... moderators can get pissy sometimes.

kingy: variables "wrap" even if they are unsigned.

unsigned char c = 255;
c++; // c now equals zero (0)

I don''t think wrapping has any real use. It''s just how the computer does its math.
"It tastes like burning..."
Advertisement
Kingy: Stoffel is telling you why what you're talking about happens. The information is useful because it explains why "if you put numbers larger than 127 in the array, they "wrap" into minus numbers." Note that, for an unsigned char, numbers larger than 255 "wrap" back to 0 and up. So (char)257 == 2.

You want a use for it? OK. Let's say you have a texture whose size is a power of two, say, 256 x 256. It is tiled. Now you want to find out, for a given x, y coordinate on that larger tiled surface, what the corresponding x, y coordinate is in your texture.

You could find x % 256, y % 256, or you could just find (char)x, (char)y, which would be much faster.

In fact, this technique is useful in dealing with anything that tiles, wraps, or repeats ad infenitum, and is a power of 4. Why a power of 4? Well, it's a power of two, for obvious reasons, because it's binary, but the size of data types increases by powers of two as well, so it has to be (n 2)2, or n4.

All integer data types - signed and unsigned char, short, int, long, and (on some compilers) __int64 (aka "long long") - "wrap around." They just "wrap" at higher numbers. Floating point data types - float and double (and, on some compilers, long double) - do not wrap, as far as I know.

Edited by - terranfury on August 5, 2001 4:47:59 PM
Thanks for your patience guys, I can indeed see now why this might be useful
“If you try and please everyone, you won’t please anyone.”

This topic is closed to new replies.

Advertisement