Question regarding bit vectors...
Hi all, I'm currently working on an assignment that keeps track of hotel room vacancies. There are 32 rooms (numbered 0-31), thus my teacher would like us to use int as a bit vector since ints are represented by 32 bits. The bit vector is initialized to 0.
My question is basically, how would I go about performing a "check-in" or a "check-out" on a room? We are only allowed to use the bitwise operators (&, |, ^, <<, >>>, ~) to manipulate the bit vector, and thus far I'm having a really hard time grasping how to perform, for example, "check in to room 4" -> (bit vector becomes 000...10000) or "check out of room 4" -> (bit vector becomes 000...00000). Then comes the issue of printing out which rooms are available or which rooms are taken :confused:
Both checking out and checking in have their own methods (empty at the moment) that take the room number as an int as its only parameter.
Any tips or help on getting started working with the bit vector in this fashion is much appreciated.
Re: Question regarding bit vectors...
Google something like 'bit mask operators' and you'll pull up tons of tutorials. Without going into too much detail, you will need ways to set the bit in a certain position and a way to check at a certain position using operators and defined values for those bits. Each bit is a power of 2, so you can use the power of 2's along with one or a combination of operators to check and/or set the bit at a particular position. This gets cumbersome when doing so on a 32-bit integer, so typically you will shift a byte off the integer by bitshifting 1-3 bytes depending upon which bits you wish to check.
Re: Question regarding bit vectors...
Quote:
Originally Posted by
copeg
Google something like 'bit mask operators' and you'll pull up tons of tutorials. Without going into too much detail, you will need ways to set the bit in a certain position and a way to check at a certain position using operators and defined values for those bits. Each bit is a power of 2, so you can use the power of 2's along with one or a combination of operators to check and/or set the bit at a particular position. This gets cumbersome when doing so on a 32-bit integer, so typically you will shift a byte off the integer by bitshifting 1-3 bytes depending upon which bits you wish to check.
Awesome, thanks. Googling "bit vector java" and similar searches resulted in little that applied to me, so I'll try your suggestion.
Re: Question regarding bit vectors...
Assuming the bits are numbered left to right: 31 to 0
Use the bit shift operator to move the value 1 to a bit position from 0 to 31. We'll call this int the bit test mask: BTM
To test if a bit is set, AND the BTM on the int containing the bits to be tested and then test if the results != 0
Or if using more than one BTM, test that the results equals the sum of the BTMs used in the AND.
To set a bit, OR with the BTM.
To clear a bit, AND with the inverse of the BTM: (-1 ^ BTM)
Use the Integer.toHexString() method to test some easy combinations of ints to get the method down.