#82787 - Natso - Tue May 09, 2006 10:03 pm
Yeah, its basically explained above, but here is a rundown:
typedef struct{
u8 warp;
}test;
test T;
//blah blah blah
/*
T.warp is now the u8. it has been given a random 1 or 0 for each of the eight bits in it. I now need to access each individually as a boolean variable...
*/
I have tried several things with pointers but none have gotten me anywhere so I will not post them online. Basically I would like to create a pointer that points to T.warp and is a bool, not a u8, so that I can work it as planned...
Any help is appreciated - Thanks in advance
- Zach
edit: I have tried to do it so the pointer is also an array but I can't get this to work. however it would be just as good if I could get it something like " *(pointer_to_warp+index)"... I've had no luck with that either though.
_________________
I'm a bomb technitian. If you see me running, try to keep up ;)
#82788 - Mighty Max - Tue May 09, 2006 10:16 pm
Set bit n:
test.warp |= (1<<n) ;
Clear bit n:
test.warp &= ~(1<<n) ;
Swap bit n
test.warp ^= (1<<n) ;
Test for bit n
if (test.warp & (1<<n)) {
...
}
However, you can not create a pointer of bool to each bit, which you can use as bit[n] = true or such, as boolean is greater then a bit.
_________________
GBAMP Multiboot
#82789 - Natso - Tue May 09, 2006 10:18 pm
... I thought that a boolean was true/false, which is in essence one 1/0 space in the data, and also that u8 was 8 of the spaces, and that 8 booleans could be masked over it.
I will try using your codes though, thanks :D
_________________
I'm a bomb technitian. If you see me running, try to keep up ;)
#82791 - Mighty Max - Tue May 09, 2006 10:31 pm
Natso wrote: |
... I thought that a boolean was true/false, which is in essence one 1/0 space in the data, and also that u8 was 8 of the spaces, and that 8 booleans could be masked over it.
I will try using your codes though, thanks :D |
Yes, boolean is only true or false, but in order to be addressable on a machine, it allways had to swallow up at least a complete byte (which is the smallest addressable unit on machines)
Then a bool is false, if all bits of its assigned byte/word are 0, otherwise its true.
A non-packed bool should be 4bytes (a word) on ARM gcc toolchain afaik.
_________________
GBAMP Multiboot
#82792 - sajiimori - Tue May 09, 2006 11:17 pm
It's important to understand how bitwise operations work, but after you do understand them, use std::bitset to simplify your code.
#82793 - Natso - Tue May 09, 2006 11:20 pm
Huzzah it worked like a charm, first try! Thanks a bunch! Also with what you say, it seems i have picked the most memory efficient way to deal with this data... I was sooo happy when I saw the warp points appear correctly, not all or none like i've been getting before.
_________________
I'm a bomb technitian. If you see me running, try to keep up ;)
#82804 - Dwedit - Wed May 10, 2006 4:56 am
Note that unless you need to pack to SRAM, or are very tightly constrained for RAM, or use a large array, it may be better to not address bits individually, since the code to address them may take around 4 more instructions than addressing the bytes. If you do it a lot, the extra instructions may add up to more than the size of the bit array. As a possible size optimization, try to access bit arrays as few times as possible, maybe wrap access inside a function call.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#82806 - ScottLininger - Wed May 10, 2006 5:35 am
Yeah, unless you're hitting memory constraints, an array of ints will work better, even if you're "wasting" a bunch of bits. An int will happily contain 1 or 0 and work in boolean expressions like...
Code: |
if (warpPoint[0] || warpPoint[1]) {
//do something;
} |
#82863 - Natso - Wed May 10, 2006 10:12 pm
Basically I just needed to have the u8 hold the 8 values, because in a System(solar system) in the game, there can be up to eight warp points in each system, and i noticed I could save SRAM by using a u8, and so I did. I don't need to access them excessively(only when the sector is drawn, and that is only during the player's turn and it isn't exactly often in terms of the speed of the processor.
Coincidintally, though, I am packing my SRAM (i've got about 800 bytes left for emergency purposes, the rest is full), and my RAM just ran out recently and I had to change my text from u8 arrays to using #define in their place
_________________
I'm a bomb technitian. If you see me running, try to keep up ;)
#82871 - ScottLininger - Wed May 10, 2006 11:12 pm
Ahh... SRAM. Say no more. Yeah, so bit masking is the way to go.
-Scott