Core List → Affinity Mask
Affinity Mask → Core List
What Is CPU Affinity?
CPU affinity is the practice of binding a process or thread to one or more specific CPU cores. Instead of letting the scheduler run a task anywhere, affinity gives the operating system a preferred set of cores. This can reduce cache misses, improve predictability, and help isolate latency-sensitive workloads.
Why Use a CPU Affinity Calculator?
Many systems represent affinity with a bitmask. Each bit corresponds to a logical CPU index:
- Bit 0 controls CPU 0
- Bit 1 controls CPU 1
- Bit 2 controls CPU 2
- ...and so on
Manually converting between core lists (like 0,2,4-7) and masks
(0xF5) is error-prone. This calculator does the conversion both ways instantly.
How the Math Works
Core List to Mask
For each selected CPU core n, set bit n to 1. The final mask is the sum of all powers of two:
mask = Σ (2^coreIndex)
Example: cores 0,1,4 → 2^0 + 2^1 + 2^4 = 1 + 2 + 16 = 19, which is
0x13 in hex and 10011 in binary.
Mask to Core List
Read the binary mask from right to left. Wherever a bit is 1, that CPU is included in the affinity set.
Common Use Cases
- Pinning real-time services to reduce jitter
- Isolating noisy background jobs from latency-critical applications
- Performance testing with repeatable CPU placement
- Tuning containers, VMs, and game servers
Linux and Windows Examples
Linux (taskset)
You can launch a process with a specific mask:
taskset 0xF ./my_app
Or use core list syntax:
taskset -c 0,2,4-7 ./my_app
Windows (start /affinity)
Windows accepts hex affinity masks as well:
start /affinity F my_app.exe
Practical Tips
- Remember that logical CPUs include hyper-threads on SMT systems.
- Avoid pinning everything too tightly; leave room for kernel and I/O work.
- Measure before and after changes using your real workload.
- When in doubt, start with a small dedicated core set and iterate.
FAQ
Does affinity always improve performance?
Not always. It can help with cache locality and latency, but overly strict pinning can reduce scheduler flexibility. Benchmark on your own machine.
What if I select a core index above my CPU count?
This tool validates indices when you provide total logical CPUs, and it will report an error if any selected core is out of range.
Why are hex masks so common?
Hex is compact and maps cleanly to binary (4 bits per hex digit), making large affinity masks easier to read and share.