Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What am I doing wrong defining single bit?

What am I doing wrong defining single bit?

Scheduled Pinned Locked Moved C / C++ / MFC
linuxhelpc++hardware
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I am Using GCC running on Linux / Ubuntu gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) I need / like to have a single bit variable / flag passed to / from SPI hardware. Single bit defined // need for SPI /I2C typedef unsigned char bit : 1; // single bit Implemented char x; static bit b; // defined in generic def header Compiler error - what initializer is missing? In file included from ../src/MODULES/M_SPI/CASPI.h:53:0, from ../src/MODULES/M_SPI/CASPI.cpp:34: /media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token typedef unsigned char bit : 1; // single bit ^ ../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type bit C_SPIClass::I2CTX(unsigned char d) { ^ Thanks for any help. Cheers

    R L 2 Replies Last reply
    0
    • V Vaclav_

      I am Using GCC running on Linux / Ubuntu gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) I need / like to have a single bit variable / flag passed to / from SPI hardware. Single bit defined // need for SPI /I2C typedef unsigned char bit : 1; // single bit Implemented char x; static bit b; // defined in generic def header Compiler error - what initializer is missing? In file included from ../src/MODULES/M_SPI/CASPI.h:53:0, from ../src/MODULES/M_SPI/CASPI.cpp:34: /media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token typedef unsigned char bit : 1; // single bit ^ ../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type bit C_SPIClass::I2CTX(unsigned char d) { ^ Thanks for any help. Cheers

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      There IS no initializer given there. It needs to be set to a value like this : static bit b = 0;

      1 Reply Last reply
      0
      • V Vaclav_

        I am Using GCC running on Linux / Ubuntu gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) I need / like to have a single bit variable / flag passed to / from SPI hardware. Single bit defined // need for SPI /I2C typedef unsigned char bit : 1; // single bit Implemented char x; static bit b; // defined in generic def header Compiler error - what initializer is missing? In file included from ../src/MODULES/M_SPI/CASPI.h:53:0, from ../src/MODULES/M_SPI/CASPI.cpp:34: /media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token typedef unsigned char bit : 1; // single bit ^ ../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type bit C_SPIClass::I2CTX(unsigned char d) { ^ Thanks for any help. Cheers

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        You can only have bit fields in a struct or union, it would be physically stupid to have them outside a struct or union because they couldn't be packed anyhow you might as well pick any normal type. Your somewhat pointless definition (it saves zero memory space) would be

        typedef struct {
        unsigned bit : 1; // single bit
        } BIT;

        The initialize would be

        static BIT bit = { 0 };
        or
        static BIT bit = {.bit = 0}; // C11 and above standard

        The only point to bit fields is to save space or make naming and offsets easier in particular for hardware registers. The other recommendation I make is only define them as sign or unsigned you don't have to assign a normal C type the compiler internally knows how to cast bit fields to do operations on them. Usually what you are doing on hardware registers is assigning the bits to match something like an 8 bit definition

        typedef struct __attribute__((__packed__, aligned(1))) {
        unsigned Clk : 1; // clk bit
        unsigned Bit : 1; // Data bit
        unsigned CS : 1; // Chip select
        unsigned _reserved: 5; // 5 unused bits
        } MYREGISTER;

        In the above you have 8 bits packed into a byte so it saves space carrying them.

        In vino veritas

        V 1 Reply Last reply
        0
        • L leon de boer

          You can only have bit fields in a struct or union, it would be physically stupid to have them outside a struct or union because they couldn't be packed anyhow you might as well pick any normal type. Your somewhat pointless definition (it saves zero memory space) would be

          typedef struct {
          unsigned bit : 1; // single bit
          } BIT;

          The initialize would be

          static BIT bit = { 0 };
          or
          static BIT bit = {.bit = 0}; // C11 and above standard

          The only point to bit fields is to save space or make naming and offsets easier in particular for hardware registers. The other recommendation I make is only define them as sign or unsigned you don't have to assign a normal C type the compiler internally knows how to cast bit fields to do operations on them. Usually what you are doing on hardware registers is assigning the bits to match something like an 8 bit definition

          typedef struct __attribute__((__packed__, aligned(1))) {
          unsigned Clk : 1; // clk bit
          unsigned Bit : 1; // Data bit
          unsigned CS : 1; // Chip select
          unsigned _reserved: 5; // 5 unused bits
          } MYREGISTER;

          In the above you have 8 bits packed into a byte so it saves space carrying them.

          In vino veritas

          V Offline
          V Offline
          Vaclav_
          wrote on last edited by
          #4

          Thanks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups