-
>> operator in C??
Hi,
Here's the problem, hope someone can help:
I have read in 2 bytes from a file and have to decipher info from these two bytes based on bits!
My inofrmation is stored in the first 7 bits of the 2 bytes and there is other info in the last 9 bits of the 2 bytes. I was told that the >> or << operators can be used? Can anyone show me how I could use them to store say the first 7 bits into a variable called date? Thanks in advance.
-
Shifting is simple:
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
unsigned x,
date,
bit = 21845U;
date = bit << 9;
for ( x = 0; x < 16U; x++ )
printf ( "%d", !!( date & ( 1 << (unsigned)x ) ) );
return EXIT_SUCCESS;
}
Output: 0000000001010101
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
unsigned x,
date,
bit = 21845U;
date = ( ( bit >> 7 ) << 7 );
for ( x = 0; x < 16U; x++ )
printf ( "%d", !!( date & ( 1 << (unsigned)x ) ) );
return EXIT_SUCCESS;
}
Output: 0000000010101010
-Prelude
-
hello Prelude :)
can you please tell me how does the program work ?
i am intrested how to convert from int to 0&1 :)
thank you,,,