Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2005
    Messaggi
    116

    [C] significato dell´istruzione >>=

    ciao a tutti,
    non so cosa faccia esattamente questa istruzione:
    temp >>= 15;

    Il contesto dove l´ho trovata é questo:

    codice:
    int my_random(int max)  {   //  Return Random Int Between 0 and max
    	unsigned long temp;
      	temp = (unsigned long) rand();
    	temp *= (unsigned long) max;
    	temp >>= 15;
    	return (int) temp; 
    }

    Help
    ---------------------------------------------------------------
    Libera il mondo: usa linux!

    Neither MS-Word nor MS-PowerPoint attachments please: http://www.gnu.org/philosophy/no-word-attachments.html

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Effettua lo "shift a destra" di 15 bit del valore indicato ...

    Sai cos'e' lo "shift a destra" di un bit?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2005
    Messaggi
    116
    ok, grazie mille!!!

    Si, lo so. Ma perché fa 15 shift?

    Il fatto é che il numero restituito da rand varia a seconda della variabile RAND_MAX.

    Io ho trovato un altro codice simile che fa cosí:

    codice:
    static int my_random(int max) //  Return Random Int Between 0 and max
    {
    	long temp;
    #if defined(__OS_Win_) //For Windoze 
    	temp = (long) rand();
    #else // This is for unix 
    	temp = random() >> 16;
    #endif
    	temp = temp * (long) max;
    	temp = temp >> 15;
    	return (int) temp; 
    }
    
    static MY_FLOAT noise_tick() //  Return random MY_FLOAT float between -1.0 and 1.0
    {
    	MY_FLOAT temp;
    	temp = my_random(32767) - 16384;
    	temp *= 0.0000610352;
    	return temp;
    }



    Mi chiedevo se puó andare bene anche questo codice che io ho scritto, (il mio obiettivo é
    ottenere un numero che varia tra -1 e 1), e vorrei sapere se é meglio degli altri e perché....help!

    codice:
     
    //noise maker
    float noise_tick()  //  Return random float between -1.0 and 1.0
    {
    	float output;
    	output = (float)rand() - RAND_MAX/2;
    	output *= ONE_OVER_RANDLIMIT;
    	return output;
    }

    dove ONE_OVER_RANDLIMIT é pari a 1/RAND_MAX


    Vi ringrazio
    ---------------------------------------------------------------
    Libera il mondo: usa linux!

    Neither MS-Word nor MS-PowerPoint attachments please: http://www.gnu.org/philosophy/no-word-attachments.html

  4. #4
    Utente di HTML.it
    Registrato dal
    Jul 2005
    Messaggi
    116
    scusa, il codice corretto dovrebbe essere senza il /2 altrimenti si hanno numeri tra -0.5 e 0.5

    quindi:
    codice:
    //noise maker
    float noise_tick()  //  Return random float between -1.0 and 1.0
    {
    	float output;
    	output = (float)rand() - RAND_MAX;
    	output *= ONE_OVER_RANDLIMIT;
    	return output;
    }
    dove ONE_OVER_RANDLIMIT é pari a 1/RAND_MAX


    Se lo faccio girare su diverse macchine va bene o no?
    Che problemi ci possono essere?
    Perché altre persone avevano usato il codice che ho postato prima?

    Grazie!
    ---------------------------------------------------------------
    Libera il mondo: usa linux!

    Neither MS-Word nor MS-PowerPoint attachments please: http://www.gnu.org/philosophy/no-word-attachments.html

  5. #5
    Utente di HTML.it
    Registrato dal
    Jul 2005
    Messaggi
    116
    emm... ho fatto un errore :-)

    no cosí ottengo solo numeri negativi tra -1 e 0.
    ---------------------------------------------------------------
    Libera il mondo: usa linux!

    Neither MS-Word nor MS-PowerPoint attachments please: http://www.gnu.org/philosophy/no-word-attachments.html

  6. #6
    Utente di HTML.it
    Registrato dal
    Jul 2005
    Messaggi
    116
    Il codice giusto é questo (testato ;-) ):


    codice:
    //noise maker
    float noise_tick()  //  Return random float between -1.0 and 1.0
    {
    	float output;
    	output = (float)rand() - RAND_MAX/2.;
    	output *= ONE_OVER_RANDLIMIT*2;
    	return output;
    }
    Rimangono le domande di prima.....help
    ---------------------------------------------------------------
    Libera il mondo: usa linux!

    Neither MS-Word nor MS-PowerPoint attachments please: http://www.gnu.org/philosophy/no-word-attachments.html

  7. #7
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,304
    Originariamente inviato da frodo_jedi
    Si, lo so. Ma perché fa 15 shift?
    Perchè queste due istruzioni sono perfettamente identiche:

    codice:
    temp >>= 15;
    
    e
    
    temp = temp >> 15;
    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.