Shift << not working for me
Rsadeika
Posts: 3,837
Been working on this all morning, and I am not getting any closer to a correct solution. Basically this code segment is receiving a string value of "4\n" which somehow ends up being 32704, I am doing a shift << 4 to get the value back to 4, but the shift process keeps yielding 0. What am I missing here?
Thanks
Ray
Thanks
Ray
if(!strcmp(inBuff,"gofore")) { print("%s",inBuff); // debug check value = gofore writeStr(xbee,"#"); // signal to send next value pause(50); readStr(xbee,inBuff,40); // incoming value is "4\n" int b = atoi(inBuff); // inBuff is 32704 int d = b << 4; // Why isn't this shifting print(d); // debug check value of d = 0, expecting 4 //drive_goto(d*8,d*8); writeStr(xbee,"#"); // send control value }
Comments
Ray
presuming you are using 32 bit int...
to strip off the low order 16 bits you may want to use b>>16 instead, no?
Sorry Ray,
All I saw in your listing for printing the result of your shift is this which won't work:
print(d); // debug check value of d = 0, expecting 4
Ray
Output:
I just tried the suggestion of b >> 16, and when I have 'print("%d\n", d)', that comes up as 0. Do I have to turn something on in SimpleIDE?
Ray
Output:
Ray
Ok,
I think we have established clearly that readStr(), atoi(), and shif in the examples I posted are doing the right thing.
The d prior to drive_goto(d*8, d*8) will be 64 according to the example. If you remove the shift, then d will be 4.
It sounds like the question that you're really asking is how to convert the string representation of a number to the binary (int) representation?
http://www.cplusplus.com/reference/cstdlib/atoi/
Ray
Ray
Looking at the Python code again I noticed this: I was thinking that it was not getting the necessary '\n', but I guess I was wrong. Once I removed that line, now the interpreter seems to be working. I guess this brainstorming session really works for me, a lot of Thanks to everybody. Now I should have a very crude PropGCC interpreter to work with the PyGUI program ready, in a relatively short time.
Ray
I think it really helps to break a problem down into small functional units when looking for bugs. Such "unit testing" is valuable for scripted like Python as well as compiled languages like C (i.e., syntax checking). Functional problems are harder to find than syntax issues of course.
One thing you can do if you're not sure about what a serial port is doing is to make a "loopback" .... That is have the target processor echo back whatever is being received in quotes back to your program so you can tell what's happening. Using quotes will make it obvious whether or not a newline is being received. This is just for testing of course.