syntax question binary to decimal (i think)

This is a discussion on syntax question binary to decimal (i think) within the C Programming forums, part of the General Programming Boards category; Hi, I am new to C programming and I am messing with a PIC breadboard project and trying to understand ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    syntax question binary to decimal (i think)

    Hi,

    I am new to C programming and I am messing with a PIC breadboard project and trying to understand the code associated with the USB pc interface.

    I think the output when read from the chip is binary format and is placed in the data_in array below. The following converts that input to a decimal value that represents the binary equivalent. Can anyone explain what this line of code is doing ?

    Specifically the ">>" , "<<" and the "& 0x03" as well as the pipe (|) etc. I think the 0x03 etc are hexadecimal but I would like to understand what this is doing and how the relative values are chosen to have the desired effect of creating a decimal representation of a binary string.
    Code:
          return_data = (
     	        ((data_in[0] >> 4) & 0x03) |  
                    ((data_in[0] << 2) & 0x04) |  
                    ((data_in[0] >> 3) & 0x18));
    many thanks,

    Pete.
    Last edited by ear9mrn; 01-09-2011 at 01:11 AM. Reason: update title

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick syntax question
    By klmdb in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2009, 07:25 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Syntax question: &(++x)
    By yoshiznit123 in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 11:40 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21