Conversion
Lesson 67: Using word() Conversion
Learn how word() converts values to 16-bit unsigned integers, how wrap behavior works, and when to apply it to embedded timing and packed data.
Progress indicator
Lesson 67 of 67
Learning Objectives
- Understand what word() conversion does in Arduino.
- Learn exact syntax and how to apply word() safely.
- Differentiate word() casting from declaring a word variable.
- See wrap-around behavior when values exceed 16-bit range or are negative.
- Use word() for compact timing, counters, and packed status fields.
- Avoid common mistakes with mixed signed/unsigned comparisons.
Concept Explanation
What is word() Conversion
word() converts a value into a 16-bit unsigned integer (0–65535).
It is useful when you need compact positive values for timers, counters, or packed data fields.
word() Syntax
word result = word(value);
word narrowed = word(largeNumber);How word() Works
- Evaluate the source expression.
- Keep the lower 16 bits; higher bits are dropped.
- Result is always 0–65535 (no negative values).
- Use the converted value in later comparisons and output.
Negative inputs wrap because the bit pattern is reinterpreted as unsigned.
Type Casting Concept
Casting forces the value into the 16-bit unsigned domain explicitly, instead of relying on implicit narrowing.
long x = 70000;
word y = word(x); // y becomes 4464 because 70000 - 65536 = 4464word() vs word Variable
word value;declares a 16-bit unsigned variable.word(expr)converts the expression result to 16-bit unsigned.- Combined:
word timer = word(expr);to both convert and store.
When to Use word()
- Compact counters or timer slices that never need more than 0–65535.
- Packed status words where each bit/field encodes state.
- Memory-constrained tables of small positive values.
- Interfacing with APIs or protocols that expect 16-bit unsigned values.
Range and Wrap Examples
| Input | word() result | Reason |
|---|---|---|
| 52000 | 52000 | Within 16-bit range. |
| 70000 | 4464 | 70000 - 65536 wraps to lower 16 bits. |
| -500 | 65036 | Bit pattern interpreted as unsigned. |
Example Code
This sketch converts several values with word(), shows wrap behavior, and drives an LED decision.
const int LED_PIN = 2;
long bigCount = 70000; // larger than 16-bit range
long pulseUs = -500; // negative to show wrap
long adcRaw = 52000; // mid-range value
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
word count16 = word(bigCount);
word pulse16 = word(pulseUs);
word scaled = word(adcRaw / 2);
bool ledState = (count16 > 30000) && (pulse16 != 0);
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
Serial.print("bigCount=");
Serial.print(bigCount);
Serial.print(", word(bigCount)=");
Serial.print(count16);
Serial.print(", word(pulseUs)=");
Serial.print(pulse16);
Serial.print(", scaled=");
Serial.println(scaled);
delay(800);
}Example Code Explanation
word(bigCount)wraps 70000 into 4464 (keeps lower 16 bits).word(pulseUs)shows how negative values become large positives.word(adcRaw / 2)narrows a mid-range value safely.ledStateuses converted values to decide ON/OFF.- Serial prints show both signed sources and converted word values.
What Happens Inside
- Expression is evaluated in the source type.
- Lower 16 bits are kept; higher bits are discarded.
- Result is stored as 0–65535 without sign.
- Logic and Serial output use the converted values.
Common Mistakes with word()
- Assuming word() clamps negatives to 0 instead of wrapping.
- Forgetting that values above 65535 will wrap, not saturate.
- Mixing signed and unsigned comparisons without a clear policy.
- Using word() when you actually need the full 32-bit range.
Best Practices for word()
- Use word() only when 0–65535 is truly sufficient.
- Document why wrap behavior is acceptable or desired.
- Log both source and converted values during development.
- Prefer explicit casts at the point of risk (before overflow-prone math).
Try it now
Open the simulator and step through how word() wraps and narrows values.