(Shift Hacks): Show numbers also in binary.

This commit is contained in:
Richard Stallman 2023-08-21 14:17:22 -04:00
parent ed6fcd7dca
commit f458b9d812
1 changed files with 15 additions and 6 deletions

21
c.texi
View File

@ -2361,19 +2361,28 @@ example, given a date specified by day of the month @code{d}, month
integer @code{date}:
@example
unsigned int d = 12;
unsigned int m = 6;
unsigned int y = 1983;
unsigned int d = 12; /* @r{12 in binary is 0b1100.} */
unsigned int m = 6; /* @r{6 in binary is 0b110.} */
unsigned int y = 1983; /* @r{1983 in binary is 0b11110111111.} */
unsigned int date = (((y << 4) + m) << 5) + d;
/* @r{Add 0b11110111111000000000}
@r{and 0b11000000 and 0b1100.}
@r{Sum is 0b11110111111011001100.} */
@end example
@noindent
To extract the original day, month, and year out of
@code{date}, use a combination of shift and remainder.
To extract the day, month, and year out of
@code{date}, use a combination of shift and remainder:
@example
/* @r{32 in binary is 0b100000.} */
/* @r{Remainder dividing by 32 gives lowest 5 bits, 0b1100.} */
d = date % 32;
/* @r{Shifting 5 bits right discards the day, leaving 0b111101111110110.}
Remainder dividing by 16 gives lowest remaining 4 bits, 0b110.} */
m = (date >> 5) % 16;
/* @r{Shifting 9 bits right discards day and month,}
@r{leaving 0b111101111110.} */
y = date >> 9;
@end example
@ -7170,7 +7179,7 @@ set_message (char *text)
if (text[i] == 0)
return;
@}
fatal_error ("Message is too long for `message');
fatal_error ("Message is too long for `message'\n");
@}
@end example