Choose Your Data Alignment
00:00 Choose Your Data Alignment. Although each border pattern can be defined by as little as four bits, because there are sixteen different combinations of border sides, you’ll need to use the whole byte with eight bits to represent a border.
00:14 That’s because a single byte is the smallest unit of information in the digital world. It’s the same story with your square’s role, which would require only three bits to describe the seven unique values.
00:26 In total, you really only need seven bits per square to faithfully represent all possible combinations of borders and roles in your maze. Now, it’s entirely up to you how you want to align data comprising the square’s border and role. If you don’t mind being a little wasteful, then one way would be to keep those two pieces of information as a binary word made up of two consecutive bytes. However, that would effectively allocate more than twice the storage space necessary.
00:55 A far better option is to fit both numbers, which correspond to borders and roles, on a single byte. You can do this by shifting the bits of one number, a few places to the left and computing their bitwise union with the other number.
01:34 When you look at the resulting bit pattern from its right edge, you see that the first four bits are the same as the bits of the border. The next three bits to the left come from the role. Finally, the most significant bit remains unused, so its value will always be zero.
Otherwise, Python would interpret this last line of code as an attempt to make a compound
Border member instead of an integer number. The underlying
enum.IntFlag data type overwrites the bitwise
OR operator to mean something other than its usual definition.
03:02 The combined border and role is your square value, which you’ll keep in the array of numbers in the file body. You can decipher the border by superimposing a bitmask on top of the square value to isolate specific bits.
03:52 Note that you can express the same bitmask in different number systems. It’s customary to use the hexadecimal system because it’s usually the most compact. You can see the binary, hexadecimal, and decimal equivalent of the mask on-screen.
04:25 You’ve just defined a custom binary file format that will allow you to share your mazes with friends or keep them on disk to yourself. As you can see, there’s a lot to consider when designing a custom binary file format.
Become a Member to join the conversation.