A little problem in C

Discussion in 'Programming' started by Rikesh, Sep 12, 2009.

  1. #1
    I'm stuck in the last part of my program:

    I have an array of 8 integers(that represent 8 bits) and I need to increment it by one.

    that is, this is my array:
    x[0]=0
    x[1]=1
    x[3]=1
    x[4]=0
    .
    .
    .
    x[7]=1
    
    Code (markup):
    increment part:
    00000000 ---> 00000001
    11111000 ---> 11111001
    11111001 ---> 11111010
    Code (markup):
    Anyone can help?

    Thanks :)
     
    Rikesh, Sep 12, 2009 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int bits[8];
      
      bits[0] = 1;
      bits[1] = 1;
      bits[2] = 0;
      bits[3] = 0;
      bits[4] = 0;
      bits[5] = 1;
      bits[6] = 1;
      bits[7] = 1;
    
      int finalbyte = 0;
      int i;
      
      for (i=0; i<8; i++)
      {
        if (bits[i] == 1)
        { 
          finalbyte |= (1<<i);
        }
      }
    
      ++finalbyte;
      
      printf("%d", finalbyte);
      printf("\n");
    
      system("PAUSE");	
      return 0;
    }
    
    
    
    Code (markup):
    finalbyte is byte value of the bits in the array + 1.
    If you need the actual values in the array updated let us know.
     
    LogicFlux, Sep 12, 2009 IP
  3. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's some code that assumes your bits will represent a byte value between 0 and 255 inclusive (the size of an unsigned char).

    
    #include <stdio.h>
    #include <stdlib.h>
    
    void copyBits(int, int[]);
    unsigned char getChar(int[]);
    
    int main(int argc, char *argv[])
    {
      int bits[8];
      
      ///*
      bits[0] = 1;
      bits[1] = 1;
      bits[2] = 0;
      bits[3] = 0;
      bits[4] = 1;
      bits[5] = 1;
      bits[6] = 0;
      bits[7] = 1;
      //*/
      
      int i;
      unsigned char newbyte = getChar(bits);
      
      printf("newbyte = %d\n", newbyte);
    
      ++newbyte;
      
      printf("newbyte incremented = %d\n", newbyte);
        
      printf("original bits: ");
      for (i=7; i>=0; i--)
      {
        printf("%d", bits[i]);
      }
      printf("\n");
        
      copyBits(newbyte, bits);
      
      printf("updated bits:  ");
      for (i=7; i>=0; i--)
      {
        printf("%d", bits[i]);
      }
      printf("\n\n");
      
      system("PAUSE");	
      return 0;
    }
    
    void copyBits(int byte, int bits[])
    { 
      int i;
      
      for (i=0; i<8; i++)
      {
        if (byte & (1<<i))
        {  
           bits[i] = 1;
        }
        else
        { 
          bits[i] = 0;
        }
      }
    }
    
    unsigned char getChar(int bits[])
    { 
      unsigned char newbyte = 0;
      int i = 0;
      
      for (i=0; i<8; i++)
      {
        if (bits[i] == 1)
        { 
          newbyte |= (1<<i);
        }
      }
      
      return newbyte;
    }
    
    
    Code (markup):
    What will be the byte value range? Do you need to support negative numbers? I think how you do this all depends on the ranges you have to support.
     
    LogicFlux, Sep 12, 2009 IP
  4. Rikesh

    Rikesh Active Member

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #4
    Thanks mate :)
     
    Rikesh, Sep 12, 2009 IP
  5. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No prob. But this code isn't perfect. If the array contains all ones or a value of 255 and you try to increment using the unsigned char I used, at least with my compiler on my system it will go back to zero. You could probably just change every instance of "unsigned char" with "int" and change getChar to getInt and it might be safer.
    But then I'm not really a C guru and I'm kind of drunk. :)
     
    LogicFlux, Sep 12, 2009 IP
  6. Rikesh

    Rikesh Active Member

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #6
    I'm still a noob in C.
    Anyway, I found a simpler way using a for-loop and break.
     
    Rikesh, Sep 13, 2009 IP