Click here to Skip to main content
15,921,028 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralVisual C++ and Standard C++ Pin
RanBlade25-Sep-03 13:53
RanBlade25-Sep-03 13:53 
GeneralRe: Visual C++ and Standard C++ Pin
Taka Muraoka25-Sep-03 14:09
Taka Muraoka25-Sep-03 14:09 
GeneralRe: Visual C++ and Standard C++ Pin
RanBlade25-Sep-03 14:15
RanBlade25-Sep-03 14:15 
GeneralRe: Visual C++ and Standard C++ Pin
RanBlade25-Sep-03 14:17
RanBlade25-Sep-03 14:17 
GeneralRe: Visual C++ and Standard C++ Pin
Taka Muraoka25-Sep-03 14:31
Taka Muraoka25-Sep-03 14:31 
GeneralRe: Visual C++ and Standard C++ Pin
Joe Woodbury25-Sep-03 18:16
professionalJoe Woodbury25-Sep-03 18:16 
GeneralSimple operation with window minimized... Pin
Anonymous25-Sep-03 13:30
Anonymous25-Sep-03 13:30 
GeneralC and memory help Pin
Sirrius25-Sep-03 12:39
Sirrius25-Sep-03 12:39 
Lets see, how do I explain this. I need to read in a char and a double for each struct and print out its binary representation within that struct. For some reason when I read in the char and double for each struct all at once (like it is coded below) the binary representation gets messed up. If I only read in for one struct it seems to print out the binary rep. just fine, whether or not packed or unpacked.
There seems to be a memory problem when reading from all the structs at once.
Maybe one of you more experienced programmers can make some sense of this code and what might be the problem with printing out the binaries like they should be.
Thank you all for you help.

#include <stdio.h>
typedef unsigned  int  DWORD32;

#pragma pack(push,4)
typedef struct
{
	char i;
	double x;
}unPacked;
#pragma pack(pop)





typedef struct {
  char i;
  double x;
}packed_structure_t;



typedef struct
{
	DWORD32
			i :8,
	    	x :23;

}bitField32;





void PrintBits(int size, unsigned char *bytes)
{
  int j=0;
  int on = 1;
  int off = 0;
  int t;
  char *b = ((char *) &bytes);
  int i;
  
  for (i=0; i<size; ++i)
  {
	 
	 printf("\nByte%d  ",j++);
	 
	 for(t=7; t>=0; --t)
		  
		if(bytes[i] & (1 << t))
			
			  printf("%d",on);
		  else
			  printf("%d",off);
  }
  printf("\n\n");
 }


int main(int argc, char *argv[])
{
  
  bitField32 s;
  unPacked n;
  packed_structure_t p;
  

 
for(;;) {
    int i;
    double x;
	


  
	printf("\n");
    printf("Type in an int and a double: ");
    scanf(" %d %lf", &i, &x);
   
	printf("\n");
    p.i=i;
    p.x=x;
	printf("Packed binary representation of a structure:\n");
	PrintBits(sizeof(p),(unsigned char*)&p);
	
   
	
	n.i=i;
	n.x=x;
	printf("Unpacked binary representation of a structure:\n");
	PrintBits(sizeof(n),(unsigned char*)&n);

	
   
	
	s.i=i;
	s.x=x;
	printf("Bit Field representation of a structure:\n");
	PrintBits(sizeof(s),(unsigned char*)&s);
	


  }

}

GeneralRe: C and memory help Pin
Larry J. Siddens25-Sep-03 13:03
Larry J. Siddens25-Sep-03 13:03 
GeneralRe: C and memory help Pin
Sirrius25-Sep-03 14:46
Sirrius25-Sep-03 14:46 
GeneralRe: C and memory help Pin
Larry J. Siddens25-Sep-03 15:02
Larry J. Siddens25-Sep-03 15:02 
GeneralID of a child dialog Pin
Antti Keskinen25-Sep-03 11:42
Antti Keskinen25-Sep-03 11:42 
GeneralRe: ID of a child dialog Pin
Antti Keskinen25-Sep-03 11:49
Antti Keskinen25-Sep-03 11:49 
GeneralApp is crashing when trying to restore Brushes and Fonts Pin
Terry O'Nolley25-Sep-03 9:56
Terry O'Nolley25-Sep-03 9:56 
GeneralRe: App is crashing when trying to restore Brushes and Fonts Pin
Alvaro Mendez25-Sep-03 10:19
Alvaro Mendez25-Sep-03 10:19 
GeneralRe: App is crashing when trying to restore Brushes and Fonts Pin
Terry O'Nolley25-Sep-03 10:46
Terry O'Nolley25-Sep-03 10:46 
GeneralRe: App is crashing when trying to restore Brushes and Fonts Pin
Michael Dunn25-Sep-03 10:32
sitebuilderMichael Dunn25-Sep-03 10:32 
GeneralRe: App is crashing when trying to restore Brushes and Fonts Pin
Terry O'Nolley25-Sep-03 11:06
Terry O'Nolley25-Sep-03 11:06 
QuestionConversion from DB_NUMERIC to double? Pin
Rene De La Garza25-Sep-03 9:50
Rene De La Garza25-Sep-03 9:50 
AnswerRe: Conversion from DB_NUMERIC to double? Pin
Steve S25-Sep-03 22:45
Steve S25-Sep-03 22:45 
Generalspeaker volume too low Pin
Binayak25-Sep-03 9:40
Binayak25-Sep-03 9:40 
GeneralRe: speaker volume too low Pin
David Crow25-Sep-03 9:45
David Crow25-Sep-03 9:45 
GeneralRe: speaker volume too low Pin
Binayak25-Sep-03 10:14
Binayak25-Sep-03 10:14 
GeneralRe: speaker volume too low Pin
David Crow25-Sep-03 10:19
David Crow25-Sep-03 10:19 
GeneralRe: speaker volume too low Pin
Binayak25-Sep-03 10:22
Binayak25-Sep-03 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.