SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By dchrislee
#150310
Hi everyone,

Have a problem with the following code:
Code: Select all
  unsigned char dat0 = 0xFF;
  sprintf(temp, "DATA: 0x%04X\n", dat0);
  SendString(temp);
  
  long int _dat00 = (dat0 << 24);
  sprintf(temp, "DAT00: %ld, (%d)\n", _dat00, sizeof(long int));
  SendString(temp);// send it to terminal via UART
result:
Code: Select all
 DATA: 0x00FF
 DAT00: 0, (4)
Question: Why _dat00 is 0, it should be 0xFF000000!
IDE: Arduino 1.0.1, AVR GCC: avr-gcc.EXE (WinAVR 20081205) 4.3.2
Code: Select all
arduino-1.0.1\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=101 -ID:\setup-media\dev\avr\arduino-1.0.1-windows\arduino-1.0.1\hardware\arduino\cores\arduino -ID:\setup-media\dev\avr\arduino-1.0.1-windows\arduino-1.0.1\hardware\arduino\variants\standard C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp -o C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.o 

arduino-1.0.1\hardware\tools\avr\bin\avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p -o C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.elf C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\mp3info.cpp.o C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.o C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\core.a -LC:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp -lm 

arduino-1.0.1\hardware\tools\avr\bin\avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.elf C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.eep 
D:\setup-media\dev\avr\arduino-1.0.1-windows\arduino-1.0.1\hardware\tools\avr\bin\avr-objcopy -O ihex -R .eeprom C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.elf C:\Users\VAIO-L~1\AppData\Local\Temp\build7377534116887203675.tmp\sketch_aug20a.cpp.hex 
By n1ist
#150313
Try casting dat0 into a long. I think the arithmetic is being done as ints (16 bits) and then promoted to a long,
Code: Select all
  long int _dat00 = ((long int)dat0 << 24);