Simple training demonstration to show the use of union and structure

Dependencies:   USBDevice mbed

Fork of OddExample2 by Jon Fuge

main.cpp

Committer:
jf1452
Date:
2013-11-26
Revision:
11:085c63023906
Parent:
10:6c0cd51df4df

File content as of revision 11:085c63023906:

/*******************************************************************************
* This program demonstrates determines if a number is odd or even              *
* Jon Fuge V1.0 26/11/2013 First issue of code                                 *
*******************************************************************************/
#include "mbed.h"
#include "USBSerial.h"

union byte // Define new type “BYTE”
{
    char Byte; // Use this to map a byte
    struct {
       char Bit0: 1; // map individual
       char Bit1: 1; // bits onto the
       char Bit2: 1; // mapped byte.
       char Bit3: 1;
       char Bit4: 1;
       char Bit5: 1;
       char Bit6: 1;
       char Bit7: 1;
    };
};

USBSerial serial; // Virtual serial port over USB. Use Teraterm as the interface

int main() {
   byte cMyNumber;      // Declare variable cMyNumber
   cMyNumber.Byte = 53; // Set it to an odd number

   wait (10); // Wait 10 seconds to connect port
   
   if (cMyNumber.Bit0 == 1)
      serial.printf("%i is odd\n\r", cMyNumber.Byte);
   else
      serial.printf("%i is even\n\r", cMyNumber.Byte);

   for(;;) {} // Loop forever
}