Jump to content
IGNORED

Counting Bits


KevKelley

Recommended Posts

I hadn't read any way to do this and was wondering if there is a way to count the bits in a variable, like see how many 1s or 0s are in it so that if I were to do something like flip some bits I could count just how many I turned on or off.

 

One idea I had was to have a data table with each entry on the table a number from 0-8 so that if a variable is checked it would just pull the number from the data table to be used elsewhere in the program.  

Link to comment
Share on other sites

I responded in the other topic. I don't think there's a simple way to count bits other than counting them one at a time like I describe there.

 

Edit: if you used a data table to store the number of bits, the table size would actually have to be 256 bytes to contain all possible bit combinations.

Edited by Karl G
Link to comment
Share on other sites

13 minutes ago, Karl G said:

I responded in the other topic. I don't think there's a simple way to count bits other than counting them one at a time like I describe there.

 

Edit: if you used a data table to store the number of bits, the table size would actually have to be 256 bytes to contain all possible bit combinations.

Sorry about starting a new topic. After I posted that other one I thought it veered too off-topic. I realized if I did a data table it would take that many bytes but I figured if I had space to spare it may be worth it but after overcomplicating what I intended to do I think I found a much simpler method. 

 

The only other thing I was wondering was remembering which bits I turn off or on, but I assume that would probably require a couple variables for checking or comparing to what it was. 

 

I guess I had an idea and then started playing around with these ideas until I found something that worked but I still wondered if it was at all possible.

Link to comment
Share on other sites

On 5/8/2022 at 1:58 PM, KevKelley said:

I hadn't read any way to do this and was wondering if there is a way to count the bits in a variable, like see how many 1s or 0s are in it so that if I were to do something like flip some bits I could count just how many I turned on or off.

 

One idea I had was to have a data table with each entry on the table a number from 0-8 so that if a variable is checked it would just pull the number from the data table to be used elsewhere in the program.  

Not sure if I understand it but would it be possible to xor the bits without counting so it only flips the bits you need flipped? 

Genericvariable= Genericvariable ^ %00010101 

Link to comment
Share on other sites

9 hours ago, ultima said:

Not sure if I understand it but would it be possible to xor the bits without counting so it only flips the bits you need flipped? 

Genericvariable= Genericvariable ^ %00010101 

That would be part of what I was thinking. I was just wondering if there was a way to then count how many variables get flipped. So my idea was if Genericvariable equaled %00010101 then I would turn off and count them. That is why I considered a look up table 

  • Like 1
Link to comment
Share on other sites

32 minutes ago, ultima said:

You could try building a finite statemachine so you wouldn't need to count.

In the game I was working on one player would turn off x number of playfield pixels and the other turn them back on. That was simple enough - when I turned one off or on I just increased or decreased another variable. 

 

In another part of the game I tried something similar I ran into an issue. I would still keep track of the pixels added but I had something drop and change the playfield Variables on or off but in doing this it would flip more than needed and mess up my counts. I tried a whole bunch of checks but those were not perfect but that had me wonder methods of counting or checking. 

 

I find these topics of interest because while I may have changed my approach in this game I still want to try and learn more.

 

What exactly is a finite statemachine?

Link to comment
Share on other sites

I like to think of a finite statemachine as a programs brain turned into a variable. Sections of code can then be activated by the current state of said variable thru a quick on...goto statement at the start of each page of code. Imagine a chameleon who has all colors stored in pages of code and for him to blend against another color he has to update that variable that accesses the code. Sorry that was a terrible example lol.

  • Like 1
Link to comment
Share on other sites

7 hours ago, ultima said:

I like to think of a finite statemachine as a programs brain turned into a variable. Sections of code can then be activated by the current state of said variable thru a quick on...goto statement at the start of each page of code. Imagine a chameleon who has all colors stored in pages of code and for him to blend against another color he has to update that variable that accesses the code. Sorry that was a terrible example lol.

Okay. I think I understand as I have done similar conditional statements to kind of activate dormant code, so to speak. 

 

Part of me feels as if I was getting close to a solution to my problem earlier before I switched directions (I opted to use a missile versus numerous pfread/pfpixel statements). The direction I was going was basically counting every pixel turned off and then as each pfpixel coordinate was read checking to see if it was off or on and then if off leaving it off and not counting it. But I felt my method was imperfect because I used an x and y coordinate and then converted it to pfpixel coordinates and sometimes when converting it it would snag the wrong pfpixel so when my loop would play out it would screw the math up. 

 

Nothing a little trial and error wouldn't fix but at least I got better understanding of things in the process. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...