Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Question regarding bit vectors...

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding bit vectors...

    Quote Originally Posted by copeg View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default 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.

Similar Threads

  1. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  2. Vectors
    By mgutierrez19 in forum Collections and Generics
    Replies: 4
    Last Post: March 3rd, 2010, 10:46 AM
  3. Replies: 3
    Last Post: November 15th, 2008, 06:17 AM