Thread: >> operator in C??

  1. #1
    Unregistered
    Guest

    >> 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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    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,,,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  2. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  4. overloaded >> operator problem
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2005, 03:27 PM
  5. C++ >> Standard Library >> Header File
    By hrk2006 in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2003, 08:30 PM