will c# compiler recognize when dividing or multiplying by base 2

say if i tell it to divide by 8, or even 8f, will it recognize this as a bitshift-able and save the calculation cost?

8f, definitely not. Floats aren’t stored like regular binary numbers and you can’t bitshift them.

For ints, maybe if it’s a constant.

In either case, this will literally never make a difference you’ll be able to discern, so don’t worry about it.

My brief Google search found this comment:

1 Like

thank you for the answer guys. i thought bitshifting was significantly faster, hance searching by layer in unity so much faster.

Dividing adds so much simplicity to design, but is costly, so i always think thrice before implementing it.

Bitshifting for layers is a little bit of a special case. Bitshifting there isn’t so much for the math itself, but because each bit is associated within one of the layers. It’s like a specialized array (but one that can be processed faster because they can all fit within the processor’s register in a single cycle and be compared with a bitwise OR or bitwise AND.)

The alternatives to that with equivalent functionality would be:

  1. An array of 32 bools, which takes up 4*32 bytes in memory due to how C# addresses bools.
  2. A list of ints (the layers which are “on”), which would require O(N) cycles to check for a match in the list.

Put another way, for layers, bitshifting isn’t being used as a speedy replacement for multiplication; it’s the actual most sensible way to represent the operation being done, which is putting bits into the correct locations to match the desired layers.

So this is saying otherwise, tested in 2012. It is saying bitshifting is incredibly faster.

I was hoping to greatly simplify things by giving every entity a single integer ID that can be deconstructed for information, because I want to save every rock, tree, and building and NPC. This would require (best as I can imagine) a combination of division and Modulo to extract the digits.

For sanity, I would understand a base 10 system, but for speed, I would be better off with a base 2 system. Plus I think I could cram more info in base 2.

Havent got that far quite yet anyway. I’ll mull it over and research some more.

Never optimize until you have met these conditions:

  1. you actually have a performance problem
  2. you have actually used the profiler (or some other means) to measure it
  3. you know what the actual slow code is

Don’t speculate what it is. That’s not engineering. Acting on speculative impulses will make a horrible spaghetti mess out of your code, with bitfields and shifting and oh my… I have seen codebases where this has happened and it is HORRIBLE. Trust me, just write it cleanly and straightforward until you have an actual performance problem, and then most likely the profiler will tell you a far more useful place to optimize.

What kind of information?

Procedural seeds. Procedural unwrapping instructions.

or STR, CHA, INT and How many rats still needed to kill to reach level 4.

It is already a procedurally generated map, that I am serializing, will have to switch to A* project for navMesh serialization, try as hard as I did to do it with Unity.

I’m just thinking now that I have learned to serilaize in binary… …I could store history a la dwarf fortress (never played) in tiny packets, with a little creativity, and store it for good.

I know using bitshifting to store data is not unheard of. Holisitc3D offers a class on it on Udemy. Unfortunately, I don’t like marketers that use NLP, because yes I am coding my own game, but it is not easier than you think, you know, I shouldn’t do anything when faced with marketing manipulation techniques.

But I’ve been mulling the architecture of it for years now, even before I heard it. Especially for something like a deep strategy or world game. Where bones of the fallen can sit undisturbed for years, but not be forgotten, and everything that is generated, is saved.