Hello,
Recently I've begun work on a maze generator for adults and children alike. Basically the algorithm calls for a 2d array with multiple values for each index in that array. Each index represents a square in the maze and each square has a value for whether it has been visited and which of the 4 possible walls is up.
Since this is a binary set of variables (wall standing vs wall removed) and (visited vs unvisited) I was hoping to implement a sort of single byte value. I'm told that a byte has 8 bits to work with and those 8 bits seem like more than enough possible boolean variables.
I need to store the following variables in a single bit value inside the Byte.
1 Has a North Wall (true or false)
2 Has an East Wall (true or false)
3 Has a South Wall (true or false)
4 Has a West Wall (true or false)
5 Has been visited (true or false)
So would it be possible to have a single 8 bit value to match?
North East South West Visited unused unused unused
I'm fairly green to the notion of using bits.
A. Given this format, how would I set the bits in the byte?
B. Given this format, how would I check the values in the byte for each boolean variable using "if" loops or "case" loops?
Initially I got an algorithm working using objects to hold the cell data but it became memory intensive and sluggish. I was told that bit operations are extremely fast and using a single byte to describe all 5 boolean values seems to be memory efficient too.
Please assist me. I'm very new to this level of memory manipulation.
Recently I've begun work on a maze generator for adults and children alike. Basically the algorithm calls for a 2d array with multiple values for each index in that array. Each index represents a square in the maze and each square has a value for whether it has been visited and which of the 4 possible walls is up.
Since this is a binary set of variables (wall standing vs wall removed) and (visited vs unvisited) I was hoping to implement a sort of single byte value. I'm told that a byte has 8 bits to work with and those 8 bits seem like more than enough possible boolean variables.
I need to store the following variables in a single bit value inside the Byte.
1 Has a North Wall (true or false)
2 Has an East Wall (true or false)
3 Has a South Wall (true or false)
4 Has a West Wall (true or false)
5 Has been visited (true or false)
So would it be possible to have a single 8 bit value to match?
North East South West Visited unused unused unused
I'm fairly green to the notion of using bits.
A. Given this format, how would I set the bits in the byte?
B. Given this format, how would I check the values in the byte for each boolean variable using "if" loops or "case" loops?
Initially I got an algorithm working using objects to hold the cell data but it became memory intensive and sluggish. I was told that bit operations are extremely fast and using a single byte to describe all 5 boolean values seems to be memory efficient too.
Please assist me. I'm very new to this level of memory manipulation.