Simple training demonstration to show the use of union and structure

Dependencies:   USBDevice mbed

Fork of OddExample1 by Jon Fuge

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002 * This program demonstrates determines if a number is odd or even              *
00003 * Jon Fuge V1.0 26/11/2013 First issue of code                                 *
00004 *******************************************************************************/
00005 #include "mbed.h"
00006 #include "USBSerial.h"
00007 
00008 union byte // Define new type “BYTE”
00009 {
00010     char Byte; // Use this to map a byte
00011     struct {
00012        char Odd:    1; // this is the same as Bit0
00013        char Others: 7; // Just pad the rest of the bits
00014     };
00015 };
00016 
00017 USBSerial serial; // Virtual serial port over USB. Use Teraterm as the interface
00018 
00019 int main() {
00020    byte cMyNumber;      // Declare variable cMyNumber
00021    cMyNumber.Byte = 53; // Set it to an odd number
00022 
00023    wait (10); // Wait 10 seconds to connect port
00024    
00025    if (cMyNumber.Odd == 1)
00026       serial.printf("%i is odd\n\r", cMyNumber.Byte);
00027    else
00028       serial.printf("%i is even\n\r", cMyNumber.Byte);
00029 
00030    for(;;) {} // Loop forever
00031 }