question about what this line might be trying to accomplish

This is a discussion on question about what this line might be trying to accomplish within the C++ Programming forums, part of the General Programming Boards category; I'm not entirely sure about this line of code, its from the original gl quake's source code. Carmack does a ...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    question about what this line might be trying to accomplish

    I'm not entirely sure about this line of code, its from the original gl quake's source code. Carmack does a lot of funny things at the bit level and I have no friggin clue what they are supposed to be

    the actual part of this is the
    rand()&3) & 1

    the entire context is this:
    any clue what the bitwise and operator is being used for? If i remember correctly if bit A and bit B are 1, then the resulting bit is also a 1, otherwise its a zero if either are 0?
    Code:
    	if ( ((rand()&3) & 1) ||  abs(deltay)>abs(deltax))
    	{
    		tdir=d[1];
    		d[1]=d[2];
    		d[2]=tdir;
    	}
    Oh and this is actually monster code, I guess it randomly chooses a dijrection to move in somehow?

    Thanks if you can help explain this to me

    EDIT: is doing operations on variables faster on the bit level?
    Last edited by Silvercord; 03-21-2003 at 04:42 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    ((rand()&3) & 1)
    Why would he do that? That's the same as (rand() & 1). maybe he isn't so almighty as we first thought...

    Anyway, I'd guess that rand() snippet creates a random True/False statement with a 50/50 chance for each.
    What the whole part is doing i have no clue. Swaps two values when something occurs.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    so all rand() does is produce true/false? I honestly did not know that. I never understood how rand()%upperbound works. Like rand()%20 creates a random number to 20. I know what the modulus operator does but it still doesn't make sense completely.

  4. #4
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    //rand() produces true/false with 50/50 chance of each
    //& bitwise operator produces 1 when A bit AND B bit are 1, otherwise it is 0
    //11 is binary for 3
    //01/00 & 11 -> 01/00
    //01&01 -> 01
    //00&01 -> 00

    that still doesn't make sense in terms of any useful functionality

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    what he is doing is masking out the 0 bit (in a bad way as magos pointed out since ((rand()&3) & 1) is the same as (rand() & 1) )
    he might be using it because mod or floating point division can be expensive operations.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Silvercord
    so all rand() does is produce true/false? I honestly did not know that. I never understood how rand()%upperbound works. Like rand()%20 creates a random number to 20. I know what the modulus operator does but it still doesn't make sense completely.
    I didn't say rand(), I said rand() snippet, and by that I refered to the code you posted.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    What other languages allow you to manipulate bits directly/one at a time?

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Silvercord
    What other languages allow you to manipulate bits directly/one at a time?
    c, c++, java, and probably many more have bitwise operators such as...

    &, |, ~, ^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on grepping multiple words in a line
    By Overworked_PhD in forum Linux Programming
    Replies: 6
    Last Post: 04-11-2009, 11:12 PM
  2. fseek question regarding line number
    By DB66 in forum C Programming
    Replies: 1
    Last Post: 10-18-2008, 07:23 PM
  3. beginners question: write onto command line
    By xximranxx in forum Windows Programming
    Replies: 7
    Last Post: 05-05-2007, 08:49 AM
  4. Command Line arguments question
    By micpi in forum C Programming
    Replies: 7
    Last Post: 04-24-2007, 08:46 PM
  5. Command Line parameters question
    By Cbuild in forum C Programming
    Replies: 4
    Last Post: 11-26-2003, 07:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21