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.
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
*/
#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');
}
if((c&Mask)==1) putchar('1'); // not work
else if((c&Mask)==0)putchar('0');
putchar(c & Mask ? '1' : '0');
echo47 said:That is both broken and clumsy. Mask equals 128, so (c&Mask)==1 is always false.Code:if((c&Mask)==1) putchar('1'); // not work else if((c&Mask)==0)putchar('0');
That is correct and concise.Code:putchar(c & Mask ? '1' : '0');
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.
#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');
}
#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;
}
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; }
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]
#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');
}
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.
#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;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?