9 years, 5 months ago.

Can you please explain what led = 0*F means in your code

  1. include "mbed.h"

BusOut leds(LED1,LED2,LED3,LED4); AnalogIn ain(p20);

int main () { while (1) { leds = 0xF * ain.read(); } }

Question relating to:

AnalogIn program for mbed NXP LPC11U24 LPC11U24, M0

1 Answer

9 years, 5 months ago.

0xF is in decimal 15, which is in binary 0b1111. So what it does is set the four lowest bits as '1', which enables all LEDs.

To clarify: In c or c++ "0x" tells the compiler that what follows is a hexadecimal number (base 16). F in hex represents 15. Similarly if a number starts with 0 it tells the compiler that it is an octal number.

This means that int A = 10; sets A to a value of 10.

int A = 0x10; sets A to a value of 16

int A = 010; sets A to a value of 8

Why do this? Because when you are driving hardware directly you sometimes need to care about the binary value. But using binary directly results in numbers that are too long and hard to read, that's where hexadecimal comes in. Converting decimal numbers to binary is painful, for anything over about 15 most people have to reach for a calculator. Converting hexadecimal to binary is relatively straight forward, each character is 4 binary bits making it a lot easier to deal with.

Octal is hardly ever used. I've mainly mentioned it as a warning not to start integer numbers with a 0, that can give you some hard to track down bugs.

posted by Andy A 05 Dec 2014