Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High Order and Low-order byte

The prototypes for getchar() and putchar() are:

int getchar(void);

int putchar(int c);

As ,its prototype shows, the getchar() function is declared as returning an integer.However, you can assign this value to a char variable, as is usually done, because the character is contained in the low-order byte.(The high-order byte is normally zero.)

Similary in case of putchar(),even though it is declared as taking an integer parameter you will generally call it using a character argument.Only the low order byte of its parameter is actually output to the screen.

What do you mean by high order and low order bytes?

like image 857
rimalroshan Avatar asked Nov 05 '17 02:11

rimalroshan


2 Answers

In C, the size of an int is implementation defined, but is usually 2, or 4 bytes in size. The high-order byte would be the byte that contains the largest portion of the value. The low-order byte would be the byte that contains the smallest portion of the value. For example, if you have a 16-bit int, and the value is 5,243, you'd write that in hex as 0x147B. The high order byte is the 0x14, and the low-order byte is the 0x7B. A char is only 1 byte, so it is always contained within the lowest order byte. When written in hex (in left-to-right fashion) the low-order byte will always be the right-most 2 digits, and the high-order byte will be the left-most 2 digits (assuming they write all the bytes out, including leading 0s).

like image 130
user1118321 Avatar answered Oct 08 '22 03:10

user1118321


I think the best analogy for this would be to look at decimal numbers.

Although this isn't literally how things work, for the purposes of this analogy let's pretend that a char represents a single decimal digit and that an int represents four decimal digits. If you have a char with some numeric value, you could store that char inside of an int by writing it as the last digit of the integer, padding the front with three zeros. For example, the value 7 would be represented as 0007. Numerically, the char value 7 and the int value 0007 are identical to one another, since we padded the int with zeros. The "low-order digit" of the int would be the one on the far right, which has value 7, and the "high-order bytes" of the int would be the other three values, which are all zeros.

In actuality, on most systems a char represents a single byte (8 bits), and an int is represented by four bytes (32 bits). You can stuff the value of a char into an int by having the three higher-order bytes all hold the value 0 and the low-order byte hold the char's value. The low-order byte of the int is kinda sorta like the one's place in our above analogy, and the higher-order bytes of the int are kinda sorta like the tens, hundreds, and thousands place in the above analogy.

like image 37
templatetypedef Avatar answered Oct 08 '22 03:10

templatetypedef