r/CodingHelp • u/ADFormer • 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!
•
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_valueJust note that this will warp around to 0 at first, not 1. For example:
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.
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 & 0xffandsecond = 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" tooBut 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/braaaaaaainworms 21d ago
Do you mean a division by 256?
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/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.
1
u/Fun_Army2398 22d ago
%