How do the bit operators work??
How can I transform decimals into binarys or hexadecimals and viceversa????
What are bit operators good for??
How do the bit operators work??
How can I transform decimals into binarys or hexadecimals and viceversa????
What are bit operators good for??
---Programming is like roaming, you never know where you'll end at------
If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??
[email protected]
>How can I transform decimals into binarys or hexadecimals and viceversa????
Converting a decimal value to hexadecimal is very simple. Just use printf with the %x flag. Decimal to binary is a bit more complex, but not a great deal. The following code illustrates one way to do it:
printBin is a simple recursive function that checks if the value passed to it is 0. If it isn't then it will call itself again with the same value bit shifted right one space, this is done with the >> operator. When all of the original value has been shifted away the remaining value will be 0 and the function prints the value of the bit. All of the work is done in the print, where the first bit in the number is checked with &, and the appropriate character is printed.Code:#include <stdio.h> static void printBin ( unsigned val ) { if ( val != 0 ) printBin ( val >> 1 ); (void)putchar ( ( val & 1 ) != 0 ? '1' : '0' ); } int main ( void ) { printBin ( 27U ); return 0; }
>How do the bit operators work??
This has been answered before, try searching the boards so that I don't have to type it all again.
>What are bit operators good for??
See previous answer.
-Prelude
My best code is written with the delete key.