C bitwise operator issue

Your source for all things Atmel.

Moderator: phalanx

Post Reply
dchrislee
Posts: 4
Joined: Mon Feb 13, 2012 11:56 pm

C bitwise operator issue

Post by dchrislee » Mon Oct 08, 2012 12:25 pm

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 

n1ist
Support Volunteer
Posts: 988
Joined: Wed Mar 22, 2006 11:02 am

Re: C bitwise operator issue

Post by n1ist » Mon Oct 08, 2012 12:43 pm

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);

dchrislee
Posts: 4
Joined: Mon Feb 13, 2012 11:56 pm

Re: C bitwise operator issue

Post by dchrislee » Mon Oct 08, 2012 12:46 pm

n1ist wrote: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);
Yes, you're right: http://www.avrfreaks.net/index.php?name ... c&t=106105

Thank a lot

Post Reply