r/CodingHelp • • 22d ago

Unreal Blueprints/C++ Not even sure how to properly describe this, but how do I guess "normalize" an integer?

Hi been working on a game for a few years now and managed to stumble my way through this so far.... I've gotten stuck on the stupidest stuff.... but I have been stuck on this one for a while and worst part is I don't even know how to word this in Google to get an answer as its... a strange but either potentially simple or completely impossible thing I'm not sure XD

But what I'm trying to do is max out an integer at 255, so a 256 bit would become 1, a 512 becomes 2, 1024 becomes 4, and so on... There shouldn't be a scenario where it has to wrap over again so the max we should be having to deal with is 32,768 becoming 128 (if it even gets to that at all)

best solution I've got right now is to iterate over each bit with a for loop, "and" the integer with one of those base numbers and if that bit is detected then subtracting the desired bit from the undesired one and subtracting *that* from the original int (so like [int] - (512-2) for example) but I feel this operation is far too costly given this is already packed within 3 other (albeit equally small) loops so I was hoping there might be a much cheaper solution to this

Edit: Solved! Thx!

6 Upvotes

28 comments sorted by

•

u/AutoModerator 22d ago

Thank you for posting on r/CodingHelp!

Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app

Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp

We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus

We also have a Discord server: https://discord.gg/geQEUBm

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/Shortbread_Biscuit 22d ago edited 22d ago

It sounds like you're looking for the modulo operator. It basically divides the first number by the second and returns the remainder.

In most languages, especially in C++, C# and GDScript, it's the % operator. So it's used like this:

normalized_value = unnormalized_value % max_value

Just note that this will warp around to 0 at first, not 1. For example:

  • 255 % 256 = 255
  • 256 % 256 = 0
  • 257 % 256 = 1

Edit: on rereading your post, I'm wondering if you're actually just talking about integer division? Since 256 / 256 = 1, 512 / 256 = 2, 1024 / 256 = 4, and so on...

If that's not what you're looking for, I think you need to explain your use case a little more. Your description feels really vague.

1

u/ADFormer 22d ago

OH perfect, I've used module before for other tasks but didn't occur to me to use it for this!

2

u/pytness 22d ago

no cause he says 1024 would be 4. it would be division too

2

u/pytness 22d ago edited 22d ago

(if i understood you correctly)

first I'll tell you the direct answer:

a byte can represent from 0-255.

think of a byte as if it was a base ten digit, what you are basically saying is "how can i have an integer from 0-9, and then on the 10 have a 1, the 20 have a 2, the 40 have a 4". notice how the 1, 2 and 4 are really the number / 10.

you can do that with an integer if you read the first and second byte using a bitwise and: first = n & 0xff and second = n & 0xff00. you will probably have to do a little bit of research, read about: bytes, integer sizes, bitwise operations. but what probably will fit you better because it can handle multiple bytes is just dividing by 256. you should look up "modulo operator" too

But most importantly, what are you really trying to do? is this just a challenge for yourself, or are you trying to solve another problem and this is what you came with? (read about the xy problem)

1

u/ADFormer 22d ago

Right so I do understand bitwise operations at least to a basic level..... basically just by messing around and figuring things out

But what I'm doing is.... a bit of a long story XD but basically I'm using an 8 bit integer to control the rotation of a structure, so 1 = 0*, 2 = 45*, 4 = 90* and so on but these structures have complex relations with the structures around them so it needs to be acutely aware of what it is and what is around and right now I'm trying to save my own sanity with this one particular structure that without this solution I would need to add 255 entries to a Tmap by having the game "rotate" a mesh automatically by multiplying its "rotation ID" to get the associated mesh data that comes along with by 4, but if say its rotation was a corner piece of this larger structure facing NorthWest making its ID 193 (10000011) which rotating it by 90* by multiplying by 4 would end up as 772 (00000000111) where I actually want 7 (11100000)

I know this is a bit confusing but anything else beyond this would take a whole PowerPoint presentation or Discord VC or something XD

2

u/pytness 22d ago

seems to me you are over complicating your problem. is there any reason the ids have to be a single byte or be multiplied? If you have a "corner" with id 0, why does the id have to be multiplied by 4 instead of just adding 4 (in this case it would be 3 because you would be 0 indexing).

So: Corner_0 = 0, Corner_45 = 1, Corner_90 = 2, etc

Also, computers are really fast and have a lot of memory. Using a hashmap or just rotating on demand might not even be that slow.

1

u/ADFormer 22d ago

So its less of what they are doing and more of what is around them, its a grid based tile system so for instanced this (and I'm just now realizing I got the ID backwards) so a 1 represents there is a structure in the corresponding cell and 0 is well.... there isn't. So 1 = 0* = (1,0), 2 = 45* = (1,1), 4 = 90* = (0,1)

So basically using the same structure as an example (its a platform so I'll just call it that): in order for it to use the corner mesh facing NorthWest it needs more of the same structure at relative points (0,1), (-1,1), and (-1,0), it must be empty space at relative points (1,0), (1,-1), and (0,-1), and then (1,1) and (-1,-1) don't matter

So a northwest facing platform corner would be 00111000 (with the 2nd and 32nd bit being interchangeable)

If I did it just like that and entered in every possible combination into a Tmap for a whopping 255 entries.... it would work.... but my sanity would take a grave hit so I'm trying to find a way to reduce that 255 entires by making it figure stuff out on its own, I've already found a solution to get it to figure out those interchangeable bits which brings 255 down to 92 which is a lot better, but if I can get it to figure out rotation too then I bring 92 down to 23 (the amount of different meshes this structure has to make it all look nice all fit together)

2

u/pytness 22d ago

I still think that you are kinda over complicating it and that you are underestimating how much memory a computer has. 255 entries in a hashmap is nothing.

Also i think i just realized what you should be doing with the "IDs". I think the example you gave before and this comment has given me enough information.

You basically have a bitfield of postitions. So 1 = 0, 2 = 45, ..., 7 = 315 right?

Let's say `10000000` is 0 degrees and `01000000` is 45 degrees. notice that going clockwise from 0 to 45, the 1 is going right. And if you wanted to turn counter clockwise the bit would go left. Notice that going from 0: `10000000` to 315: `00000001`, the bit is still going left! it's just rotated around.

My bet is you need to rotate the bits. Look at the example you gave:

193 (10000011) -> 7 (11100000), you wanted to rotate clockwise twice so the bits rotated twice.

Edit: when i say "over complicating" i mean the whole rotating algorithm and such, im not a game dev

1

u/ADFormer 22d ago

Oh yeah so that is exactly what I'm trying to do and by multiplying it by 4 turns it 90*

That being said its that jump that I'm struggling with, the 10000000 to 00000001 or vice versa, I can't think of an operation that does that

As for the hash map thing.... I know the computer could do it easily.... that's not why I don't wanna do it..... I don't wanna do it that way cause I don't wanna add 255 entries to a map XD

2

u/pytness 22d ago

you can google it, rotating bits is a somewhat common problem.

If you dont want to google it, you need to use bitwise shift and a bitwise or.

1

u/ADFormer 22d ago edited 22d ago

Ye now I know what the thing I'm trying to do is called doing research on it is (or should be) easy XD

Edit: and thankfully it was indeed an easy find: https://youtu.be/S2yXCBu3NdQ?is=uKo0EPUevGRT2c5Y

2

u/gofuckadick 22d ago edited 22d ago

You're trying to rotate an 8-bit bitmask, not normalize an integer. I'm pretty sure what you're looking for is a circular bit rotation (rotate left / ROL).

Unlike a regular left shift, a circular rotation wraps the bits that overflow back around to the other side.

if say its rotation was a corner piece of this larger structure facing NorthWest making its ID 193 (10000011) which rotating it by 90* by multiplying by 4 would end up as 772 (00000000111) where I actually want 7 (11100000)

For your example, rotating 193 left by 2 bits gives 7, exactly what you want. Just a couple of corrections to the binary representations: 193 is 11000001, and 7 is 00000111.

In C++20, you can use std::rotl(value, 2) with an 8-bit unsigned integer, or do it manually:

((value << 2) | (value >> 6)) & 0xFF

If each bit represents a 45-degree direction, rotating by 45 degrees would be a 1-bit rotation, 90 degrees would be 2 bits, etc.

No loops or individual bit subtraction needed!

Edit: Just to clarify, are the decimal values (193, 7) what you're actually trying to achieve, or are the binary patterns correct (10000011 = 131 and 11100000 = 224)? Interestingly, both are circular rotations, but in opposite directions: 193 -> 7 is a 2-bit left rotation, while 131 -> 224 is a 2-bit right rotation. Either way, this is likely what you're looking for.

1

u/ADFormer 22d ago

Yes this is exactly it, thank you, now to hope Unreal has a blueprint node for this rotl XD

But both 193 and 7 are correct, the idea is shifting from the 64, 128, and 1 bit to the 1, 2, and 4 bit for a 90* rotation

2

u/gofuckadick 22d ago

Glad that helped! And if Unreal doesn't have a Blueprint node for bit rotation, you can do the same thing using basic integer math:

((Value * 4) % 256) + (Value / 64)

Just make sure the division is integer division. That gives you the same 8-bit left rotation by 2 bits, so 193 becomes 7. Should be easy enough to put together with Blueprint nodes.

1

u/ADFormer 22d ago edited 22d ago

Right so the *4 /64 thing I figured out, my struggle was in wrapping back to 1 after 128 (or vice versa)

Edit: but now that I know what its called finding the answer was easy XD https://youtu.be/S2yXCBu3NdQ?is=uKo0EPUevGRT2c5Y

2

u/pytness 22d ago

I was just answering him in another comment and just came to the same conclusion. The binary values he gave are in little endianness!

1

u/braaaaaaainworms 21d ago

Do you mean a division by 256?

1

u/ADFormer 20d ago

No turned out to be called integer rotation and what I was struggling with was bridging the jump from my max desired bit to my lowest, ended up finding a solution and closed the hood on that one yesterday :)

Happy cake day btw

1

u/braaaaaaainworms 20d ago

Division by 256 is the same as shifting right by 8. Rotation would still keep the out of range values as out of range values because the bits that were shifted out, get shifted in from the other side, as if the integer was rotated

1

u/ADFormer 20d ago

Yes so a divide by 256 was used in the equation I wrote, but it's not as simple as just dividing by 256 cause the bits that are in the desired 8-bit range would be moved where I don't want them, so I took those out, then divided by 256, then added them back

1

u/VoltexRB 20d ago

You are... Just looking for a two's logarithm minus 8? Like you want 1-255 to be that number but 255+ be log2(x)-8?

1

u/ADFormer 19d ago edited 19d ago

No so what I wanted ended up being called integer rotation, the equation is in blueprints and idk the exact C++ format for this, so I know this won't be actual code, but this is the flow I went with:

    8bit int = (Original Int*4)&255
    16bit int = (original int*4) - 8bit int
    Final int = (8bit int) [bitwise OR] ((16bit int)/256)

I did this all in one step (or at least not saving other variables, it just goes straight from A to B) in the actual code but it reads too confusing if I write it like that here as an equation XD

1

u/VoltexRB 19d ago

Yeah so that is absolutely not what you described in the post above.

1

u/Available_Safe1818 22d ago

Genius, man. You've reinvented masking or modulus!

1

u/davideogameman 22d ago

Sounds like you either want x % 256, or x & 255 if your language has a bitwise & operator. 

You may want to check behavior around negative numbers, if any.  The bitwise & will probably turn them positive, whereas the % might work like the math modulo and always give an answer between 0 and 255 or it could choose -255 to 255 - with the negatives used when x is negative.  Which one depends on the language.

Bitwise and operator only works because you asked about remainder when dividing by a power of 2.  Had you asked to say wrap every 0-5, x & 4 would not get you what you want and the modulo would be the clear, simplest option.