Re: [Circle] Bitvector Explanation please

From: Gerald Wichmann (stile@ix.netcom.com)
Date: 11/28/96


At 10:06 AM 11/29/96 +1100, you wrote:
>G'Day All,
>	  i've read the docs, but i'm still unfamiliar with bitvectors and what
>they do. i know how to impleement them, but I'm not sure how to use them in
>the game.
>For example. I had this
>#define open 0
>#define closed 1
>#define locked 2
>#define pickproof 3
>#define secret_closed 4
>#define secret_locked 5
>#define secret_pickproof 6
>
>and i was told i don't nned 5 and 6 cause 4 already 'encompasses' them.
>This is why I'm confussed, so can someone quickly explain them please
>*blush*
>
>Thanks as always.

i don't knwo if this explanation is neccessary seeing as how someone else
already responded.. but i felt his explanation wasn't in plain english as
much as i would like if i knew nothing about bitvectors.. so here it goes
for what it's worth..

as you know the computer stores information in 1's and 0's..  A single bit
is either a 1 or a 0.. to store a bigger number the computer uses a series
of 1's and 0's (several bits).. this is known as binary.  it goes something
like this..

0=0000
1=0001
2=0010
3=0011
4=0100
5=0101
6=0110
7=0111
8=1000
9=1001
10=1010
11=1011
12=1100
13=1101
14=1110
15=1111

and so on.. my demonstration here shows only a 4 bit number.. typically in
your computer an integer on most platforms these days is 32 bits (so an
integer of 1 would be stored as 00000000000000000000000000000001)..  now
that we have that down lets continue.

when people refer to bitwise operations they are talking about changing
those bits in some way.. its a little abstract i guess.  there are only a
few bit operations you can do.. you can shift left, shift right, or
compliment the bits. for example.

in the code you often see (1 << 0) or (1 << 1) or (1 << 2)..  what is this
crap you ask?  the "<<" is a shift left operation on the bits..  when you
say 1 << 0 you are saying to take the number 1 and shift the bits to the
left 0 spaces.  So if you had the number 1 you would have 0001 and doing
the shift left 0 operation you would still have 0001.  If you did a (1 <<
1) you are shifting the bits left once..  thus 0001 would turn into 0010..
if you look at the above table i wrote out a 0010 is actually equal to 2.
if you do a (1 << 2) you are shifting the bit left twice so a 0001 turns
into a 0100 which is equal to 4.  thus if you have an integer variable
stored as 1 and you shift it, you are changing the value of the integer
value depending on how you shift it.

Right shifting is the exact opposite.. like (4 >> 1) would turn 0100 into
0010 because we are shifting the bits in the number 4 right one space.  

ok so how does this apply to what you are asking above?  simple..  take a
look at your list here:

>#define open 0
>#define closed 1
>#define locked 2
>#define pickproof 3
>#define secret_closed 4
>#define secret_locked 5
>#define secret_pickproof 6

you are defining the above words to this:

open    = 0000
closed  = 0001
locked  = 0010
pickpr  = 0011

ETC..  if you look at the individual bits.. the first bit furthest to the
right is used to determine if the door is open or closed.  if it's open the
furthest bit to the right is a 0.. if it's closed the furthest bit is a 1..
thats how you tell if the door is opened or close.  the program checks the
status of that bit in the integer to determine the status of the door..
the second bit from the right is used to determine if the door is locked or
not.  so you could have the following combinations..

0000 = door open and unlocked
0001 = door closed and unlocked
0010 = door open and locked   (impossible to occur.. the program should
never assign it this)
0011 = door closed and locked

you should notice that your last four pose a problem.  if you want to make
something pickproof you need to assign another bit for that.. so when
defining pickproof you need to define it 4, not 3..  because 4 would be
0100 and thuse you'd be using the third bit in to determine if the door is
pickproof or not.

i would think you're looking to do more something like this..

0000 = open
0001 = closed
0010 = locked
0100 = pickproof
1000 = secret

notice the position of the 1 bit.. the right one determines if the door is
open or closed.. second one in determines if it's locked.. third one in
determines if it's pick-able.. and the forth determines if its secret.
thus you can have any combination of the above to create any situation.. 

if you wanted a closed secret door that is locked and pickproof the value
of the door would be:

1111

am i making sense here?  i hope so..  if you go back up and look at your
defines you will see that the way you're assigning them does not only not
make sense but you should also see what the later combinations are not
neccessary.  and why bitvectors are used in this fation.. thats where the
power lies in them.

excuse my spelling as it is late :)

-Gerald

+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST