how to change one bit of an array

General discussion on mikroC.
Post Reply
Author
Message
saharul
Posts: 489
Joined: 08 Jul 2010 08:11

how to change one bit of an array

#1 Post by saharul » 18 Jan 2019 04:48

Hi,

i try to randomly change one bit of an array. i tried below code but no success with error message

Code: Select all

void mutation()   // mutation funtion given a pointer to array of chromes
{char array1[9];
char array1[9];
char array2[9];
char array3[9];
char array4[9];
char array5[9];
char array6[9];

     int random;
   int row,col;                                 
   random=rand()%50;                  //random value is between ( 0 - 49 )
    
   if (random==25)    // Suppusiong Probability of mutation is 2 % 
   {
    col=rand()%6;                                   // random column (gene) choosing 
      row=rand()%4;                                   // random row ( chrome ) choosing
    array*[row]*[col] =~array*[row]*[col] ;}//error
    array*(row)*(col) =~array*(row)*(col) ;}//error
    array*[row]*[col] ='1';}//error
    array*(row)*(col) ='1';}//error
    array*row*col ='1';}//error
   array*[row]*[col] =1;}//error
    array*(row)*(col) =1;}//error
    array*row*col =1;}//error


User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: how to change one bit of an array

#2 Post by jovana.medakovic » 18 Jan 2019 12:43

Hello,

If you want to change some bit of 2-dimensional array, first your declaration is not correct. You need to write, for example:

char array[9][9];

After that, you can use this way:

array[row][com] = ~array[row][com];

or this:

array[row][com] = 1;

Kind regards,
Jovana

Mihajlo555
Posts: 9
Joined: 11 Oct 2018 20:40

Re: how to change one bit of an array

#3 Post by Mihajlo555 » 18 Jan 2019 19:55

Hello. Also you have to take care of function rand(). after every reset of your program, rand() will always the same number. Because of that you have to place srand() function before rand(), and the parameter of srand() should be some time variable. For Interrupt you can use timer0, timer1 or other.

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: how to change one bit of an array

#4 Post by saharul » 23 Jan 2019 06:15

Thanks jovana and Mihajlo,

i tried your suggestion as code below but with no success, at GLCD the unknown character appears at the respective column needed to be changed.

Code: Select all

 int random;
   int row,col;  
int block=0,i=0; int j=0;
unsigned char ran,tErr[2];
unsigned char num1, num2;
char array[9][9];
char arrayaku[9][9];
/*char array1[9];
char array2[9];
char array3[9];
char array4[9];
char array5[9];
char array6[9];  */


// Glcd module connections
char GLCD_DataPort at PORTD;

sbit GLCD_CS1 at LATB0_bit;
sbit GLCD_CS2 at LATB1_bit;
sbit GLCD_RS  at LATB2_bit;
sbit GLCD_RW  at LATB3_bit;
sbit GLCD_EN  at LATB4_bit;
sbit GLCD_RST at LATB5_bit;

sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction  at TRISB2_bit;
sbit GLCD_RW_Direction  at TRISB3_bit;
sbit GLCD_EN_Direction  at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections

 void display(){    // if (trg2==1) {
 Glcd_Fill(0x00);                             // Clear GLCD
    Glcd_Set_Font(font5x7, 5, 7, 32);            // Change font
       // ByteToStr(txt, tErr); //floatToStr(value,tErr);
     
    Glcd_Write_Text( "a1" , 1, 0, 2);                // Write string
    Glcd_Write_Text( array[1] , 17, 0, 2);
   
     Glcd_Write_Text( "a2" , 65, 0, 2);                // Write string
    Glcd_Write_Text(array[2], 77, 0, 2);
      IntToStr(row,tErr);
     Glcd_Write_Text( "row" , 1, 1, 2);                // Write string
    Glcd_Write_Text( tErr , 17, 1, 2);
       IntToStr(col,tErr);
      Glcd_Write_Text( "col" , 65, 1, 2);                // Write string
    Glcd_Write_Text( tErr , 77, 1, 2);
    
     Glcd_Write_Text( "aku" , 1, 2, 2);                // Write string
      Glcd_Write_Text( arrayaku , 17, 2, 2); 
      Glcd_Write_Text( "a6" , 65, 2, 2);                // Write string
      Glcd_Write_Text( array[6] , 77, 2, 2);  
      
     Glcd_Write_Text( "n1" , 1, 4, 2);                // Write string
    //Glcd_Write_Text( num1, 17, 4, 2); 
      Glcd_Write_Text( "n2" , 65, 4, 2);                // Write string
    //Glcd_Write_Text( num2, 77, 4, 2); 
         IntToStr(block,tErr);
    Glcd_Write_Text( "j" , 65, 6, 2);                // Write string
    Glcd_Write_Text( tErr, 77, 6, 2);
    // Glcd_Write_Text( "r" , 1, 7, 2);                // Write string
    //Glcd_Write_Text( ran, 27, 7, 2);
    IntToStr(j,tErr);
    Glcd_Write_Text( "j" , 65, 7, 2);                // Write string
    Glcd_Write_Text( tErr, 77, 7, 2);
      Delay_ms(3000);              //   }
        }


void FourBitsToStr( unsigned char n, unsigned char *s )
{
   unsigned char mask = 0x08;

   while(mask)
   {
      if (n & mask)
        *s++ = '1';
      else
        *s++ = '0';
      mask >>= 1;
   }
   *s = '\0';
}
void BitsToStr( unsigned char n, unsigned char *s )
{
   unsigned char mask = 0x80;

   while(mask)
   {
      if (n & mask)
        *s++ = '1';
      else
        *s++ = '0';
      mask >>= 1;
   }
   *s = '\0';
}

unsigned char StrBitsToUChar( unsigned char *s, unsigned char *buffer )
{
   unsigned char mask;
   unsigned char i;
   unsigned char save = *buffer;
   if( *buffer > 15 || *buffer == 0 )
     mask = 0x80;
   else
     mask = 0x08;
   for( i = 0; s[i]; i++, mask >>= 1 )
   {
      if( s[i] == '1' )
         *buffer |= mask;
      else if( s[i] == '0' )
         *buffer &= ~mask;
      else
      {
         *buffer = save;
         return 1;
      }
   }
   return 0;
}

void main() {
  ANSELB = 0;                        // Configure PORTB pins as digital I/O
  ANSELD = 0; 
   ANSELC = 0;               // Set PORTC as output
  TRISD = 0x00;              // Set PORTD as output
  LATD=0;
   //ADC_init();
  TRISE=0;
    LATE=0;
   TRISA=0x7F; ANSELA = 0;  // LATA=0;
    TRISB=0x00;
    LATB=0;
    CM1CON0=0;
    CM2CON0=0;
     Glcd_Init();                                   // Initialize GLCD
  Glcd_Fill(0x00);
                 // Initialize UART module at 9600 bps
  Delay_ms(100);              // Initialize UART module at 9600 bps
  Glcd_Fill(0x00);
  //chrom1[1]="7";
  //num1=8;
  while (1) {
    ran = 0x0F;
    BitsToStr( ran, array[1] );
    if (StrBitsToUChar(array[1], &num1))
    {
       asm nop
    }
    ran = 0xF0;
    BitsToStr( ran, array[2] );
    if (StrBitsToUChar(array[2], &num2))
    {
       asm nop
    }
   // ran = num1 & num2;
    //BitsToStr( ran, array1 );           // array1 is "00000000"
    //ran = num1 | num2;
    //BitsToStr( ran, array2 );           // array2 is "11111111"
    
     //block=strlen(array1);  
      
      
     
     //unsigned char num1;
 strcpy(array[6],array[1]);
StrBitsToUChar(array[6], &num1);
ByteToStr(num1, array[6]);
  
     //block=array1;
   /* ran = rand()%256;
    BitsToStr( ran, array3 );
    array3[2] = 's';
    if (StrBitsToUChar(array3, &num1))   //returns error  num1 has unchanged
    {
       asm nop
    }

    ran = rand()%16;            // random 4 bits
    FourBitsToStr( ran, array4 );

    ran = rand()%16;
    FourBitsToStr( ran, array5 );

    ran = rand()%16;
    fourBitsToStr( ran, array6 ); */
    
    random=rand()%50;                  //random value is between ( 0 - 49 )
    
  // if (random==25)    // Suppusiong Probability of mutation is 2 % 
   //{
    //col=rand()%6;                                   // random column (gene) choosing
    // strcpy(arrayaku,array1); 
      //row=rand()%4;
   //  arrayaku[row][col] = ~arrayaku[row][col];
     //arrayaku[row][col] = 1;}
      row=0;
      col=4;
      strcpy(arrayaku,array[1]); 
      strcpy(array[6],array[1]); 
      arrayaku[row][col] =~arrayaku[row][col];
      row=1; 
      array[row][col] =~array[row][col];
     array[2][4] =1;
     StrBitsToUChar(array[6], &num1);
   ByteToStr(num1, array[6]);
    display();
  }
}
what went wrong?

Many Thanks,

User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: how to change one bit of an array

#5 Post by jovana.medakovic » 24 Jan 2019 10:52

Hello,

In the attachment, you can find the example where you have one array and every 3 seconds one random bit will be changed. On the GLCD you can see the result.
I hope I understand you well. If no, please, could you provide me with more details?

Kind regards,
Jovana
Attachments
Example.zip
(54.91 KiB) Downloaded 84 times

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: how to change one bit of an array

#6 Post by saharul » 25 Jan 2019 10:43

Thanks jovana,

I am writing code for some application that require a bit changes (mutation) from a decimal value. The changes is by random between 6 arrays and four bits, but to make ease for testing i set the required bit and the array.After changes made, Then the final decimal value is display at GLCD display. Below is the code which is a modification from your code, it is half way functioning/working, the only problem is when i converted back the changed bit to decimal value, it alternately display the correct decimal value(7) and 0 (wrong value) at GLCD display.

Code: Select all


// Glcd module connections
char GLCD_DataPort at PORTD;

sbit GLCD_CS1 at LATB0_bit;
sbit GLCD_CS2 at LATB1_bit;
sbit GLCD_RS  at LATB2_bit;
sbit GLCD_RW  at LATB3_bit;
sbit GLCD_EN  at LATB4_bit;
sbit GLCD_RST at LATB5_bit;

sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction  at TRISB2_bit;
sbit GLCD_RW_Direction  at TRISB3_bit;
sbit GLCD_EN_Direction  at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections
 int row,col; 
unsigned char ran;
unsigned char num1, num2;
char array[9][9] ;
//char array[9];

void FourBitsToStr( unsigned char n, unsigned char *s )
{
   unsigned char mask = 0x08;

   while(mask)
   {
      if (n & mask)
        *s++ = '1';
      else
        *s++ = '0';
      mask >>= 1;
   }
   *s = '\0';
}
void BitsToStr( unsigned char n, unsigned char *s )
{
   unsigned char mask = 0x80;

   while(mask)
   {
      if (n & mask)
        *s++ = '1';
      else
        *s++ = '0';
      mask >>= 1;
   }
   *s = '\0';
}

unsigned char StrBitsToUChar( unsigned char *s, unsigned char *buffer )
{
   unsigned char mask;
   unsigned char i;
   unsigned char save = *buffer;
   if( *buffer > 15 || *buffer == 0 )
     mask = 0x80;
   else
     mask = 0x08;
   for( i = 0; s[i]; i++, mask >>= 1 )
   {
      if( s[i] == '1' )
         *buffer |= mask;
      else if( s[i] == '0' )
         *buffer &= ~mask;
      else
      {
         *buffer = save;
         return 1;
      }
   }
   return 0;
}

void display()
{
    Glcd_Fill(0x00);                             // Clear GLCD
    Glcd_Set_Font(font5x7, 5, 7, 32);            // Change font
     Glcd_Write_Text( "a1" , 1, 0, 2);                // Write string
    Glcd_Write_Text( array[1] , 17, 0, 2);
    Glcd_Write_Text( "a1" , 1, 1, 2);                // Write string
    Glcd_Write_Text( array[1] , 27, 1, 2);
    Glcd_Write_Text( "a6" , 1, 2, 2);                // Write string
    Glcd_Write_Text( array[6] , 27, 2, 2);
}

void main() {
    ANSELB = 0;                        // Configure PORTB pins as digital I/O
    ANSELD = 0;
    ANSELC = 0;               // Set PORTC as output
    ANSELA = 0;

    TRISD = 0x00;              // Set PORTD as output
    TRISE=0;
    TRISA=0x7F;
    TRISB=0x00;

    LATD=0;
    LATE=0;
    LATA=0;
    LATB=0;

    CM1CON0=0;
    CM2CON0=0;

    Glcd_Init();                                   // Initialize GLCD
    Glcd_Fill(0x00);
    
    ran = 0xF0;
    
  while (1) {
 ran = 0x0F;
 BitsToStr( ran, array[1] );
 display();
    
    Delay_ms(3000);
 row=1;
      col=4;
 if(array[row][col] == '0'){ array[row][col] = '1'; }
     else { array[row][col] = '0'; }
StrBitsToUChar(array[1], &num1);
ByteToStr(num1, array[1]);
strcpy(array[6],array[1]); 
     StrBitsToUChar(array[6], &num1);
   ByteToStr(num1, array[6]);
display();
    
    Delay_ms(3000);
}

}
What went wrong?

can you also explain this code

Code: Select all

if (StrBitsToUChar(array1[1], &num1))
    {
       asm nop
    }
Many thanks,

User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: how to change one bit of an array

#7 Post by jovana.medakovic » 25 Jan 2019 12:42

Hi,

In the attachment, you can find the example.
I hope that this is what you want.

This If statement checks if array1[1] contains only 0 and 1 (should not have another characters).

Kind regards,
Jovana
Attachments
Example.zip
(50.66 KiB) Downloaded 81 times

Post Reply

Return to “mikroC General”