Thread: why am i not getting correct output

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    why am i not getting correct output

    I have written this program for the endian conversion but i am getting incorrect output. I am still unable to understand why?

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	unsigned int i = 1;
    	unsigned int j = 1;
    	unsigned int result;
    	unsigned int temp1 = 255, temp2 = 255;
    
    	int k  =0;
    	
    	for(k = 0; k< 31; k++)
    	{
    		result = j&i;
    		printf("%d", (result>0)? 1:0);
    		i = i<<1;
    	}
    
    	i = 1;
    
    	for(k = 0; k< 31; k++)
    	{
    		result = temp1&i;
    		printf("%d", (result>0)? 1:0);
    		i = i<<1;
    	}
    
    	i = 1;
    	temp1 = temp1<<16;
    	temp2 = temp2<<8;
    
    	i = ((i>>24)| ((i<<8) & temp1) | ((i>>8) & temp2) | (i << 24));
    
    	printf("\n After endian conversion\n");
    
    	for(k = 0; k< 31; k++)
    	{
    		result = j&i;
    		printf("%d", (result>0)? 1:0);
    		i = i<<1;
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Apart from the obvious - endian conversion; explain what this code is trying to do. What output you expect and what do you get after running it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. Getting correct output
    By blah3 in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2008, 05:54 PM
  3. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  4. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  5. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM