Simple training demonstration to show the use of bit wise And

Dependencies:   USBDevice mbed

Fork of UnionExample by Jon Fuge

Files at this revision

API Documentation at this revision

Comitter:
jf1452
Date:
Tue Nov 26 14:23:51 2013 +0000
Parent:
8:c0d351fc572f
Commit message:
Odd determination with bitwise AND

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c0d351fc572f -r 0add51bbe65d main.cpp
--- a/main.cpp	Tue Nov 26 14:11:37 2013 +0000
+++ b/main.cpp	Tue Nov 26 14:23:51 2013 +0000
@@ -1,37 +1,24 @@
 /*******************************************************************************
-* This program demonstrates the use of union                                   *
-* Word occupies the same memory space as Lo,Hi allowing conversion between     *
-* 16-bit numbers and two 8-bit numbers                                         *
+* This program demonstrates determines if a number is odd or even              *
 *                                                                              *
 * Jon Fuge                                                                     *
-* V1.0 25/11/2013 First issue of code                                          *
+* V1.0 26/11/2013 First issue of code                                          *
 *******************************************************************************/
 
 #include "mbed.h"
 #include "USBSerial.h"
  
-union uWord16 // Declare a new union
-{
-   uint16_t Word; // Word occupies two bytes
-   struct // this structure occupies the same space as Word
-   {
-      // ARM is little endian and stores the low byte first, then the high byte
-      uint8_t Lo, Hi; // Lo and Hi occupy one byte each
-   };
-};
-
 USBSerial serial; //Virtual serial port over USB. Use Teraterm as the interface
 
 int main() {
-   uWord16 Converter; // Declare Converter as a variable of type uWord16
-    
-   Converter.Word = 0x1234; // load Word with a recognisable number
+   char cMyNumber = 53; // Set it to an odd number
 
    wait (10); // Wait 10 seconds to connect port
    
-   serial.printf("uiWord=%X\n\r", Converter.Word);
-   serial.printf("cHighByte=%X\n\r", Converter.Hi);
-   serial.printf("cLowByte=%X\n\r", Converter.Lo);
+   if ((cMyNumber & 0x01) == 1)
+      serial.printf("%i is odd\n\r", cMyNumber);
+   else
+      serial.printf("%i is even\n\r", cMyNumber);
 
    for(;;) {} // Loop forever
 }
\ No newline at end of file