CPU Affinity Calculator
Convert a list of CPU cores into a taskset/sched_setaffinity hexadecimal CPU mask.
Inputs
Hexadecimal CPU Mask
0xf
taskset Command (core list)
taskset -c 0,1,2,3 <command>
Binary Mask (bit N = core N)
0000000000001111
Cores Selected
4
taskset Command (hex mask)
taskset 0xf <command>
Step by step
Values used
Total CPU Cores on System = 16; Cores to Bind To (comma-separated, 0-indexed) = 0,1,2,3
CPU mask construction
mask = OR over selected cores of (1 << core_index)
Hexadecimal CPU Mask
= 0xf
taskset Command (core list)
= taskset -c 0,1,2,3 <command>
Binary Mask (bit N = core N)
= 0000000000001111
Cores Selected
= 4
taskset Command (hex mask)
= taskset 0xf <command>
How it works
A CPU affinity mask is a bitmap where bit N set to 1 means the process/thread is allowed to run on core N — sched_setaffinity and the taskset command both accept this as a hexadecimal value. This calculator builds that bitmap from a human-readable list of core indices (as you'd read from `lscpu -e` or `nproc`), producing both the raw hex mask and a ready-to-use taskset command, useful for pinning latency-sensitive processes to specific cores (avoiding cross-core cache-line bouncing) or isolating a workload away from cores handling interrupts.
Formula
CPU mask construction
mask = OR over selected cores of (1 << core_index)
- S
- set of selected core indices
- i
- a core index in S
Frequently Asked Questions
Why pin a process to specific CPU cores at all?
Pinning avoids the scheduler migrating a process between cores, which causes CPU cache misses (each migration cold-starts the process's data in the new core's L1/L2 cache) and can hurt latency-sensitive or cache-sensitive workloads. It's also used to isolate a workload from cores that handle high interrupt load, or to dedicate cores exclusively to a real-time process.
How do I apply a CPU mask to a running process?
`taskset -p <mask_or_core_list> <pid>` changes affinity for an already-running process, while `taskset <mask_or_core_list> <command>` launches a new process directly with that affinity. Both accept either the hex mask or a comma/range core list (e.g. `0-3,8`).
Does CPU affinity guarantee exclusive use of those cores?
No — it only restricts which cores the process is allowed to run on; other processes (and the kernel) can still schedule work on those same cores unless you also use cgroups/cpuset isolation or kernel boot parameters like isolcpus to reserve cores exclusively.