LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c code? (https://www.linuxquestions.org/questions/programming-9/c-code-439736/)

alaios 04-28-2006 12:28 PM

c code?
 
What the following lines do?
uint16 t l e d s = count e r >> 4 ;



count e r = (messageˇ>s t r eng th + 60) & 0 x f f ;

nadroj 04-28-2006 12:43 PM

is the spacing and spelling correct?

alaios 04-28-2006 01:00 PM

i think so i have copy and pasted text here from a friend that need help

nadroj 04-28-2006 01:04 PM

hmm..

iv never done c, but i know intermediate c++ and advanced java.

if i were to guess, i would say that the code would do nothing because it isnt typed properly.

is there more code we could see, or can you verify this is the exact code (spacing and all) that works?

alaios 04-28-2006 01:16 PM

Ok forget the mispelled text.. just think its an elgorithm written in c/c++

paulsm4 04-28-2006 01:22 PM

Hi, Alois! Good to hear from you - I hope all is going well.

ANYWAY:
1. I think the text you cut/pasted was garbled: it is not legal C (or C++) as-is.

2. Here are the errors I got when I tried to compile:
Quote:

gcc -Wall -pedantic -g -o xx xx.c
xx.c: In function `main':
xx.c:8: warning: ISO C forbids nested functions
xx.c:8: error: syntax error before "l"
xx.c:9: error: `count' undeclared (first use in this function)
xx.c:9: error: (Each undeclared identifier is reported only once
xx.c:9: error: for each function it appears in.)
xx.c:11: error: parse error before '}' token
3. I modified it with (guesses) as to what it might have meant, along with explanatory comments:
Code:

#include <stdio.h>

typedef unsigned short uint16;

int
main ()
{
#if TOTALLY_BOGUS_SPACING
  uint16 t l e d s = count e r >> 4 ;
  count e r = (message|>s t r eng th + 60) & 0 x f f ;
#else
  /* Define variables, "counter", "strength" and "message" */
  uint16 counter = 100, strength = 10, message = 0x100;

  /* Variable "tled" takes the left (12?) bits of "counter"
    (and gets rid of the rightmost, least significant 4 bits) */
  uint16 tleds = counter >> 4;

  /* I'd parenthesize more ...  but this code snippet:
    a) adds strength + 60 (here, strength = 70)
    b) OR's the sum against message: 70 | 100 == 102
        ... and ...
    c) AND's the result against 0x100 (102 & 256 == 0) */

  counter = (message | strength + 60) & 0xff;
#endif
  return 0;
}

'Hope that helps .. PSM


All times are GMT -5. The time now is 02:16 AM.