c - Reading characters on a bit level -
I would like to be able to enter a character from the keyboard and for this I would like to display the binary code for the key in 00000001 format for example.
In addition to this, I would like to read the bits in such a way that would allow me to output at right or wrong.
For example
01010101 = false, true, false, true, false, true, false, true
I I will post the idea how I tried to do this but I do not know at all, I am still experimenting with C and this is my first taste of programming at such a low level level.
Thankyou
This code is C89:
< Code> / * We require exit * / #include & lt; Stdlib.h & gt; / * We require this CHAR_BIT * / #include & lt; Limits.h & gt; / * We require fgetc and printf * / #include & lt; Stdio.h & gt; Int main () / We need everything * / int input, index; Unsigned entry mask; Four enrichment; / * To collect an array, we tell the value of individual bits. There are approximately (8) 8 bits in a four, but it does not hurt to bring good habits in a hurry, and in C, basic types of sizes vary on different platforms. I CHAR_BIT We have a bits in bits Indicates the number of * / int bits [CHAR_BIT]; / * The easiest way to read a single character is fgetc, but keep in mind that the user should keep pressing "Return", because input is usually buffered * / input = fgetc (stdin); Printf ("% d \ n", input); / * Check the errors In C, we must always check errors / (/ input == EOF) {printf ("do not read any letters \ n"); Exit (1); } / * Type to Type To Type, type it. Strictly not necessary, we can check the bits of an int or a four, but here's how it is done. * / Inputchar = input; / * The most common way to check individual bits in a price is to use the "mask" - in this case we have only 1 bit set, which is the most important bit of all four * / mask = 1 & lt; & Lt; (CHAR_BIT - 1); / * This is a loop, takes every value of CHAR_BIT-1 from index 0, and we will read the most significant bits from the most importantly for I * / (index = 0; index & lt; CHAR_BIT ; ++ index) {/ * BitWind- & Operator & amp; How do we use the mask "Vocal and mask" will be 0 if the bit corresponding to the mask is 0, and zero if zero is 1: Ternary conditional operator and in C when you use the integer value in the boolean reference, zero -Write value is true, so why are we converting any non-zero values into 1. * / bit [index] = (ink and mask)? 1: 0; / * What we have done, * / printf ("index% d, value% u \ n", index, index and mask); / * Next bit * / mask = mask & gt; & Gt; Need a new mask for 1; } / * For each bit as 0 or 1 * / (index = 0; indexCHAR_BIT; ++ index) {printf ("% d", bits [index]); } Printf ("\ n"); / * Output for each bit "true" or "false" * / (index = 0; index; lt; CHAR_BIT; ++ index) {printf (bits [index]? "True": "wrong"); / * Fiddly part - We want a comma between each bit, but not at the end * / if (index! = CHAR_BIT - 1) printf (","); } Printf ("\ n"); Return 0; }
You do not need three loops - if you want you can combine them together, and if you are doing one of only two types of output, then you You will not need an array, you can just use each bit value because you turn it off. But I think it keeps things separate and easy to understand.
Comments
Post a Comment