Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Explaining a byte display code in Visual C++

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hai..

Anyone help me to explain this program......


------------------------------------------------------------------------------

#include <stdio.h>

void Display_Byte(const unsigned char);
/*----------------------------------------------------*/

int main()
{
unsigned int y = 0x0a0b;

printf("%-35s","Display MS byte of unsigned int y");
Display_Byte((unsigned char)(y>>8 ));

printf("%-35s","Display LS byte of unsigned int y");
Display_Byte((unsigned char)(y & 0xff));

return 0;
}
void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7;

for(i=1; i<=8; i++)
{
putchar(c & Mask ? '1' : '0');
c<<=1;
}
putchar('\n');

}

/* OUTPUT...
Display MS byte of unsigned int y 00001010
Display LS byte of unsigned int y 00001011
*/

------------------------------------------------------------------------

Thanks.........
 

Re: Visual C++

Hum seems like a code with a function to display bytes in binary (1's and 0's), this funtion is called Display_Byte. So as you can see the input data is the 16-bit HEX word y=0x0a0b, then first the program displays the MS Byte (Most significant byte) of the 16 bit word, that is the byte 0x0a, that in binary it means 00001010, and after that, the program displays the LS byte (less significant byte) that is 0x0b that in binary it means 00001011, as you canb see in the output.
Hope this explanation help you.
 

Re: Visual C++

this program is to display most siginificant bits and most siginificant bits of an integer value, as follows ;;;

#include <stdio.h> /*including standrad input output liberary/*

void Display_Byte(const unsigned char); /*function to print bite*/

int main() /*main function of the program*/
{
unsigned int y = 0x0a0b; /*preselected integer number you may change it*/

printf("%-35s","Display MS byte of unsigned int y"); /*just printing that message /*

Display_Byte((unsigned char)(y>>8 )); /*shift y by 8 bits then we send 0x0a the most significant byte of the integer number*/

printf("%-35s","Display LS byte of unsigned int y"); /*printing masg*/
Display_Byte((unsigned char)(y & 0xff)); /*send 0x0b to the lest significant byte*/

return 0;
}
void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7; /*that means 1 shift by 7 bits then we get binary 1000 0000 or hex 0x80*/

for(i=1; i<=8; i++) /* loop to print bit by bit*/
{
putchar(c & Mask ? '1' : '0'); /*if c contains 1 in position 8 print 1 else if print 0*/
c<<=1; /*shift c by ony bit to left in order to print next bit*/
}
putchar('\n');

}

/* OUTPUT...
Display MS byte of unsigned int y 00001010
Display LS byte of unsigned int y 00001011
*/
 

Re: Visual C++

jetset said:
Hum seems like a code with a function to display bytes in binary (1's and 0's), this funtion is called Display_Byte. So as you can see the input data is the 16-bit HEX word y=0x0a0b, then first the program displays the MS Byte (Most significant byte) of the 16 bit word, that is the byte 0x0a, that in binary it means 00001010, and after that, the program displays the LS byte (less significant byte) that is 0x0b that in binary it means 00001011, as you canb see in the output.
Hope this explanation help you.

Hai....

Can you explain 1 by 1........sory... because i really not understand....


---------------------------------------------------------------------------------

#include <stdio.h>

void Display_Byte(const unsigned char); // 1) why use const unsigned char
/*----------------------------------------------------*/

int main()
{
unsigned int y = 0x0a0b;

printf("%-35s","Display MS byte of unsigned int y"); //2) what's the meaning for %-35s
Display_Byte((unsigned char)(y>>8 )); // 3) How the function work

printf("%-35s","Display LS byte of unsigned int y");
Display_Byte((unsigned char)(y & 0xff)); // 4) How the function work
return 0;
}

void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;// 5) why use unsigned char
unsigned char Mask = 1<<7;

for(i=1; i<=8; i++)
{
putchar(c & Mask ? '1' : '0'); // 6) What is the for loop doing??
c<<=1; // How it work and what does it do ??
}
putchar('\n'); // 7) what is putchar();

}

-----------------------------------------------------------------------------------

Try your best.... i hope you can help me.....

Thank you...

Added after 35 minutes:

shafee001 said:
this program is to display most siginificant bits and most siginificant bits of an integer value, as follows ;;;

#include <stdio.h> /*including standrad input output liberary/*

void Display_Byte(const unsigned char); /*function to print bite*/

int main() /*main function of the program*/
{
unsigned int y = 0x0a0b; /*preselected integer number you may change it*/

printf("%-35s","Display MS byte of unsigned int y"); /*just printing that message /*

Display_Byte((unsigned char)(y>>8 )); /*shift y by 8 bits then we send 0x0a the most significant byte of the integer number*/

printf("%-35s","Display LS byte of unsigned int y"); /*printing masg*/
Display_Byte((unsigned char)(y & 0xff)); /*send 0x0b to the lest significant byte*/

return 0;
}
void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7; /*that means 1 shift by 7 bits then we get binary 1000 0000 or hex 0x80*/

for(i=1; i<=8; i++) /* loop to print bit by bit*/
{
putchar(c & Mask ? '1' : '0'); /*if c contains 1 in position 8 print 1 else if print 0*/
c<<=1; /*shift c by ony bit to left in order to print next bit*/
}
putchar('\n');

}

/* OUTPUT...
Display MS byte of unsigned int y 00001010
Display LS byte of unsigned int y 00001011
*/

Hai....

unsigned int y = 0x0a0b; /*preselected integer number you may change it*/
Why use unsigned, why not just use int??

printf("%-35s","Display MS byte of unsigned int y"); /*just printing that message /*
How the message print out, why the binary behind the message ???

Display_Byte((unsigned char)(y>>8 )); /*shift y by 8 bits then we send 0x0a the most significant byte of the integer number*/
How the program know is the most significant byte

Display_Byte((unsigned char)(y & 0xff)); /*send 0x0b to the lest significant byte*/
i still cannot understand....


void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7; /*that means 1 shift by 7 bits then we get binary 1000 0000 or hex 0x80*/

for(i=1; i<=8; i++) /* loop to print bit by bit*/
{
putchar(c & Mask ? '1' : '0'); /*if c contains 1 in position 8 print 1 else if print 0*/
c<<=1; /*shift c by ony bit to left in order to print next bit*/
}


Still cannot understand...., what's the purpose for this function....

unsigned char Mask = 1<<7; /*that means 1 shift by 7 bits then we get binary
why some time use unsigned char some time use unsigned int?? Do you know how to write the program for refer the result after compile..

putchar(c & Mask ? '1' : '0'); /*if c contains 1 in position 8 print 1 else if print 0*/
c<<=1; /*shift c by ony bit to left in order to print next bit*/
what is putchar do?? Hi...... i also don't how to ask for this... DIFFICULT TO UNDERSTANDS....




Thank you..
 

Re: Visual C++

Two requests, please:

1. You are asking very basic questions about C syntax. Please get yourself a good C book. I recommend "The C Programming Language", second edition, by Kernighan and Ritchie. You may learn that your example program is doing several unnecessary things!

2. Use the "code" button so everyone can see the indenting.
Code:
#include <stdio.h>

void Display_Byte(const unsigned char);

/*----------------------------------------------------*/

int main()
{
  unsigned int y = 0x0a0b;

  printf("%-35s", "Display MS byte of unsigned int y");
  Display_Byte((unsigned char)(y >> 8));
  printf("%-35s", "Display LS byte of unsigned int y");
  Display_Byte((unsigned char)(y & 0xff));
  return 0;
}

void Display_Byte(const unsigned char CH)
{
  unsigned char i, c = CH;
  unsigned char Mask = 1 << 7;

  for(i=1; i<=8; i++)
  {
    putchar(c & Mask ? '1' : '0');
    c <<= 1;
  }
  putchar('\n');
}
 

Re: Visual C++

#include <stdio.h>

void Display_Byte(const unsigned char);
// 1) why use const unsigned char
//constant or not it will make no difference in such case

/*----------------------------------------------------*/

int main()
{
unsigned int y = 0x0a0b;

printf("%-35s","Display MS byte of unsigned int y");
//2) what's the meaning for %-35s
//number of charcaters of the msg is 33 charcaters, so program leave 35 spaces to the left of the screen (-35) and print the string (%s) in 35 spaces to the left
so to additional blamk spaces is left before printing binary numbers

Display_Byte((unsigned char)(y>>8 ));
// 3) How the function work
// unsigned char used to convert the posted argument Y from unsigned int to unisgned char
// y=0x0b0a ;now; y>> 8 ;means; 0x000b

printf("%-35s","Display LS byte of unsigned int y");
Display_Byte((unsigned char)(y & 0xff));
// 4) How the function work
// y=0x0b0a ;now; y&0xff (normal logical and) ;means; 0x000a
0&1=0
1&1=1

return 0;
}

void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
// 5) why use unsigned char
//to avoid any minus in results

unsigned char Mask = 1<<7;

for(i=1; i<=8; i++)
{
putchar(c & Mask ? '1' : '0');
// 6) What is the for loop doing??
// to print bit by bit as charcater contains 8 bits we need loop of 8 turns to print them

c<<=1;
// How it work and what does it do ??
//that means c=c<<1; shift left by one bit then but it back to c
}
putchar('\n');
// 7) what is putchar();
// it's a function to print single charcater '\n' means new line


}

-----------------------------------------------------------------------------------

unsigned int y = 0x0a0b; /*preselected integer number you may change it*/
Why use unsigned, why not just use int??
to avoid any minus sign


printf("%-35s","Display MS byte of unsigned int y"); /*just printing that message /*
How the message print out, why the binary behind the message ???
i answered that before

Display_Byte((unsigned char)(y>>8 )); /*shift y by 8 bits then we send 0x0a the most significant byte of the integer number*/
How the program know is the most significant byte
simply we shift the integer by 8 bits right so the left is the most significant byte

Display_Byte((unsigned char)(y & 0xff)); /*send 0x0b to the lest significant byte*/
i still cannot understand....
i answered it before

void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7; /*that means 1 shift by 7 bits then we get binary 1000 0000 or hex 0x80*/

for(i=1; i<=8; i++) /* loop to print bit by bit*/
{
putchar(c & Mask ? '1' : '0'); /*if c contains 1 in position 8 print 1 else if print 0*/
c<<=1; /*shift c by ony bit to left in order to print next bit*/
}

Still cannot understand...., what's the purpose for this function....

unsigned char Mask = 1<<7; /*that means 1 shift by 7 bits then we get binary
why some time use unsigned char some time use unsigned int?? Do you know how to write the program for refer the result after compile..
char is one byte, 8 bits
iteger is two bytes, 16 bits


putchar(c & Mask ? '1' : '0'); /*if c contains 1 in position 8 print 1 else if print 0*/
c<<=1; /*shift c by ony bit to left in order to print next bit*/
what is putchar do?? Hi...... i also don't how to ask for this... DIFFICULT TO UNDERSTANDS....

i agree with you its difficult you may replace it by
if((c&Mask)==1) putchar('1');
else if((c&Mask)==0)putchar('0');


Thank you..
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Visual C++

Hai...

1) Please explain this symbol in C:

void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7;

for(i=1; i<=8; i++)
{
putchar(c & Mask ? '1' : '0'); // what function for this ? and :
c<<=1;
}
putchar('\n');

2) Izit the CH carry 1bye=8bit one time to this function "void Display_Byte(const unsigned char CH)"??

3) Now i have the idea what the function doing but how can i going to modified the program for me to easy understand because i still don't how it run...

void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7;

for(i=1; i<=8; i++)
{
if((c&Mask)==1) putchar('1'); // not work
else if((c&Mask)==0)putchar('0');
c<<=1;
}
putchar('\n');
}


}
 

Visual C++

Code:
if((c&Mask)==1) putchar('1'); // not work
else if((c&Mask)==0)putchar('0');
That is both broken and clumsy. Mask equals 128, so (c&Mask)==1 is always false.

Code:
putchar(c & Mask ? '1' : '0');
That is correct and concise.

C students are often puzzled by the conditional expression ? : because many other languages don't have it. It is very common in C programs, so learn it and make it your friend! See section 2.11 of K&R:

expr1 ? expr2 : expr3
the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is evaluated.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Visual C++

Hey "Help" you need to study a little more, please get a good C book.
 

Re: Visual C++

echo47 said:
Code:
if((c&Mask)==1) putchar('1'); // not work
else if((c&Mask)==0)putchar('0');
That is both broken and clumsy. Mask equals 128, so (c&Mask)==1 is always false.

Code:
putchar(c & Mask ? '1' : '0');
That is correct and concise.

C students are often puzzled by the conditional expression ? : because many other languages don't have it. It is very common in C programs, so learn it and make it your friend! See section 2.11 of K&R:

expr1 ? expr2 : expr3
the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is evaluated.

Hi....

Now i already understand this putchar(c & Mask ? '1' : '0'); conditional expression, Thanks.....

Can you let me know what the CH carry "void Display_Byte(const unsigned char CH)", izit CH one-shot carry 1byte= 8bits...??

Display_Byte((unsigned char)(y>>8 )); , how the function work... why Display_Byte( (??)(??) ); ??


#include <stdio.h>
void Display_Byte(const unsigned char);
/*----------------------------------------------------*/
int main()
{
unsigned int y = 0x0a0b;

printf("%-35s","Display MS byte of unsigned int y");
Display_Byte((unsigned char)(y>>8 ));

printf("%-35s","Display LS byte of unsigned int y");
Display_Byte((unsigned char)(y & 0xff));

return 0;
}
void Display_Byte(const unsigned char CH)
{
unsigned char i, c = CH;
unsigned char Mask = 1<<7;

for(i=1; i<=8; i++)
{
putchar(c & Mask ? '1' : '0');
c<<=1;
}
putchar('\n');
}

Thanks....
 

Visual C++

The example does several unnecessary things. It is silly to pass argument CH as a const (that means it cannot be changed), and then copy it into a changeable variable.

I think this is clearer. Please note that I am using C. I don't know C++.
Code:
#include <stdio.h>

void Display_Byte(unsigned char c)
{
  unsigned char mask;

  for (mask=0x80; mask; mask >>= 1)     /* 0x80, 0x40, 0x20, ... 0x01 */
    putchar(c & mask ? '1' : '0');
  putchar('\n');
}

int main(void)
{
  unsigned int y = 0x0A0B;

  printf("Display MS byte of unsigned int y  ");
  Display_Byte(y >> 8);
  printf("Display LS byte of unsigned int y  ");
  Display_Byte(y);
  return 0;
}
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Visual C++

echo47 said:
The example does several unnecessary things. It is silly to pass argument CH as a const (that means it cannot be changed), and then copy it into a changeable variable.

I think this is clearer. Please note that I am using C. I don't know C++.
Code:
#include <stdio.h>

void Display_Byte(unsigned char c)
{
  unsigned char mask;

  for (mask=0x80; mask; mask >>= 1)     /* 0x80, 0x40, 0x20, ... 0x01 */
    putchar(c & mask ? '1' : '0');
  putchar('\n');
}

int main(void)
{
  unsigned int y = 0x0A0B;

  printf("Display MS byte of unsigned int y  ");
  Display_Byte(y >> 8);
  printf("Display LS byte of unsigned int y  ");
  Display_Byte(y);
  return 0;
}

Hai....

The for loop result can be like that izit ....
 

Re: Visual C++

Hai...

1) Please explain this symbol in C:

putchar(c & Mask ? '1' : '0'); // what function for this ? and :

condition ? exp1 : exp2
means
if condition == 1 (true) execute expr1
else execute expr2

condition here is "c&Mask"
expr1 is "putchar('1');
expr2 is putchar('0');

2) Izit the CH carry 1bye=8bit one time to this function "void Display_Byte(const unsigned char CH)"??
yes it dose so


3) Now i have the idea what the function doing but how can i going to modified the program for me to easy understand because i still don't how it run...

if((c&Mask)==1) putchar('1'); // not work
else if((c&Mask)==0)putchar('0');


i was mistaken plz use
if((c&Mask)==0x80)putchar('1');
else if ((c&Mask)==00x00)putchar('0');



}
[/b]
 

Re: Visual C++

shafee001 said:
Hai...

1) Please explain this symbol in C:

putchar(c & Mask ? '1' : '0'); // what function for this ? and :

condition ? exp1 : exp2
means
if condition == 1 (true) execute expr1
else execute expr2

condition here is "c&Mask"
expr1 is "putchar('1');
expr2 is putchar('0');

2) Izit the CH carry 1bye=8bit one time to this function "void Display_Byte(const unsigned char CH)"??
yes it dose so


3) Now i have the idea what the function doing but how can i going to modified the program for me to easy understand because i still don't how it run...

if((c&Mask)==1) putchar('1'); // not work
else if((c&Mask)==0)putchar('0');


i was mistaken plz use
if((c&Mask)==0x80)putchar('1');
else if ((c&Mask)==00x00)putchar('0');



}
[/b]

Hai....

The program still not work....., now i using Microsoft Visual C++ izit any problem for this program..??

Code:
#include <stdio.h>

void Display_Byte(const unsigned char);
/*----------------------------------------------------*/

int main()
{
	unsigned int y = 0x0a0b;

	printf("%-35s","x = ");
	Display_Byte(x);

	printf("%-35s","Display MS byte of unsigned int y");
	Display_Byte((unsigned char)(y>>8));

	printf("%-35s","Display LS byte of unsigned int y");
	Display_Byte((unsigned char)(y & 0xff));

	return 0;
}
void Display_Byte(const unsigned char CH)
{
	unsigned char i, c = CH;
	unsigned char Mask = 1<<7; 
	for(i=1; i<=8; i++)
	{
//		putchar(c & Mask ? '1' : '0'); 
		if((c&Mask)==0x80)putchar('1'); 
		else if ((c&Mask)==00x00)putchar('0'); 

		c<<=1; //shift c by ony bit to left in order to print next bit
	}
	putchar('\n');
}
 

Re: Visual C++

When you posted Output.JPG, were you asking a question? What does "izit" mean?

Display_byte(x);
You never defined x. I think you meant y

else if ((c&Mask)==00x00)putchar('0');
00x00 is wrong, use 0x00

Suggestion: Enable all of your compiler's warnings and error messages. The messages will help you learn the language.
 

Re: Visual C++

echo47 said:
When you posted Output.JPG, were you asking a question? What does "izit" mean?

Display_byte(x);
You never defined x. I think you meant y

else if ((c&Mask)==00x00)putchar('0');
00x00 is wrong, use 0x00

Suggestion: Enable all of your compiler's warnings and error messages. The messages will help you learn the language.

Thanks for your suggestion.....,


The program that you show me is it (izit) the output result that i show you in .jpg file...

Code:
#include <stdio.h>

void Display_Byte(unsigned char c)
{
  unsigned char mask;

  for (mask=0x80; mask; mask >>= 1)     /* 0x80, 0x40, 0x20, ... 0x01 */
    putchar(c & mask ? '1' : '0');
  putchar('\n');
}

int main(void)
{
  unsigned int y = 0x0A0B;

  printf("Display MS byte of unsigned int y  ");
  Display_Byte(y >> 8);
  printf("Display LS byte of unsigned int y  ");
  Display_Byte(y);
  return 0;
}


Thank You....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top