Skip to main content
Senior III
February 21, 2024
Question

Assignement binary value to a variable dosn't work

  • February 21, 2024
  • 4 replies
  • 3163 views

As title suggest.

If I do

int my_var;
my_var = 0b01;

 my_var stays 0

Instead with

int my_var;
my_var = 0x01;

Everything works

 

STM32CubeIDE

Version: 1.14.1

Build: 20064_20240111_1413 (UTC)

    4 replies

    Andrew Neil
    Super User
    February 21, 2024

    Do you get any warnings/errors ?

    Binary literals aren't part of the C standard - only available by extension - so are you sure it's valid for the language spec you're using?

    CORRECTION

    Binary literals were introduced in C23:

    https://queue.acm.org/detail.cfm?id=3588242#:~:text=C23%20understands%20binary%20literals%20and,binary%20(%2201010101%22).

    So they are very new!

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    February 21, 2024

    Still, likely to be exceedingly non-portable..

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    STTwo-32
    ST Technical Moderator
    February 21, 2024

    Hello @nico23 

    I've personally tested using the STM32WB55-NUCLEO and the STM32CubeIDE V1.14.1 and this simple code:

     /* USER CODE BEGIN 1 */
    	int my_var;
    	__NOP();
    	my_var = 0b01;
     /* USER CODE END 1 */

    And that works fine (even without the __NOP() ) and set the my_var to 1 as you can see: 

    STTwo32_0-1708534413008.png

    Could you give more details about your issue.

    Best Regards.

    STTwo-32

    To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    mƎALLEm
    ST Technical Moderator
    February 21, 2024

    And if you add volatile to your declaration :

    volatile int my_var;?

     

    To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
    Tesla DeLorean
    Guru
    February 21, 2024

    That might not help prevent it from being optimized.

    Memory inspection only works if the value is in memory, not in registers.

    Get printf() / STDIO plumbing working, and printf() the value, or sprintf() via HAL_UART_Transmit()

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    mƎALLEm
    ST Technical Moderator
    February 21, 2024

    Memory inspection only works if the value is in memory, not in registers.


    Indeed, but in the current case the variable and its affectation is in the memory. 

    To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
    TDK
    February 21, 2024

    There is no chance the compiler is not handling this correctly. Likely to be you misinterpreting the data. Not enough information presented to reproduce or even verify the claim.

    "If you feel a post has answered your question, please click ""Accept as Solution""."