Thread: BIG Problems!!!

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Question BIG Problems!!!

    Hi again people!!! ...I have gone into a far smaller scope here with my problem, but after hours of messing around have literally got nowhere!!!! here is a small compilable program to show the error!

    Code:
    /**********************************************************************/
    /** file: test.c                                                     **/
    /** description: to find out how to stuff two signed short ints into **/
    /** an unsigned int and then ustuff them and print the result!       **/
    /** author: matt conway.                                             **/
    /**********************************************************************/
    #include <stdio.h>
    
    unsigned int audioEffect(short int inLeft, short int inRight){
    
      unsigned int retVal; /* return value */
      short int outLeft, outRight; /* output audio samples */
      
      /* code to process stereo audio data here */
      outLeft = inLeft; /* return input left channel audio data */
      outRight = inRight; /* return input right channel audio data */
      
      /* now return both left and right channel audio data */
      retVal = 0;
      retVal = outLeft << 16; /* stuff left audio data into int */
      retVal |= outRight;
      
      return retVal;
    
    } /* audioEffect() */
    
    
    int main(void){
    
      /* example array of audio data for left and right channels */
      short int leftAudio[15]  = {-17,11,59,51,31,42,17,-1,9,-12,-12,20,16,6,27};
      short int rightAudio[15] = {12,78,20,4,-43,5,43,-314,114,-49,-5,113,-98,-32,1};
      
      unsigned int effectedAudio; /* stores result from audioEffect() function */
      short int leftData;  /* result of audio effect for left channel  */
      short int rightData; /* result of audio effect for right channel */
      
      int i; /* loop counter */
      
      printf("example program to show my bitwise problem!\n");
    
      /* loop through audio data and apply effect */
      for (i=0;i<15;i++){
        
        effectedAudio = 0;
        effectedAudio = audioEffect(leftAudio[i],rightAudio[i]);
        
        /* now extract stereo data */
        leftData  = effectedAudio >> 16;        /* left channel data  */
        rightData = effectedAudio & 0x0000FFFF; /* right channel data */
        
        /* print audio sample before effect and after effect    */
        /* audio effect does nothing, so values should be same! */
        printf("1.Left: %8d, Right: %8d\n", leftAudio[i], rightAudio[i]);
        printf("2.Left: %8d, Right: %8d\n", leftData, rightData);    
        
      } /* loop */
      
      return 0; /* finished */
      
    } /* main() */
    and for the output...

    Code:
    example program to show my bitwise problem!
    1.Left:      -17, Right:       12
    2.Left:      -17, Right:       12
    1.Left:       11, Right:       78
    2.Left:       11, Right:       78
    1.Left:       59, Right:       20
    2.Left:       59, Right:       20
    1.Left:       51, Right:        4
    2.Left:       51, Right:        4
    1.Left:       31, Right:      -43
    2.Left:       -1, Right:      -43
    1.Left:       42, Right:        5
    2.Left:       42, Right:        5
    1.Left:       17, Right:       43
    2.Left:       17, Right:       43
    1.Left:       -1, Right:     -314
    2.Left:       -1, Right:     -314
    1.Left:        9, Right:      114
    2.Left:        9, Right:      114
    1.Left:      -12, Right:      -49
    2.Left:       -1, Right:      -49
    1.Left:      -12, Right:       -5
    2.Left:       -1, Right:       -5
    1.Left:       20, Right:      113
    2.Left:       20, Right:      113
    1.Left:       16, Right:      -98
    2.Left:       -1, Right:      -98
    1.Left:        6, Right:      -32
    2.Left:       -1, Right:      -32
    1.Left:       27, Right:        1
    2.Left:       27, Right:        1
    Shows that everytime right goes negative, when left is put into the int and then pulled back out it just results to -1 - as pointed out in a previous post earlier today!!!

    Thanks again!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your problem - you don't do the explicit convertion

    retVal |= outRight;

    when outRight is <0 it is extended to the 32 bit with the left bit (becase it is signed value)
    so the retVal get not what you wanted...

    you should first convert signed short to unsigned short before applying bitwise operations
    in this case the expanding to 32-bit value will be done with the 0es
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Big is Too Big
    By kpreston in forum C Programming
    Replies: 4
    Last Post: 10-25-2008, 10:16 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. Big problems with assignment, please help!!
    By JFonseka in forum C Programming
    Replies: 12
    Last Post: 03-25-2007, 12:16 AM
  4. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  5. Looking for some big C/C++ projects
    By C-Dumbie in forum C Programming
    Replies: 5
    Last Post: 09-16-2002, 12:18 AM