Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Bit manipulation

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Bit manipulation

UTF-8 Validation

advancedlayer I11 · Bit manipulation5 tests

Given an array data of integers where only the lowest 8 bits of each entry are meaningful, decide whether the bytes form a valid UTF-8 encoding.

A UTF-8 character is 1 to 4 bytes and its leading byte announces the length:

  • 1 byte: 0xxxxxxx
  • 2 bytes: 110xxxxx
  • 3 bytes: 1110xxxx
  • 4 bytes: 11110xxx
  • every continuation byte must look like 10xxxxxx

Any other leading pattern (such as 10xxxxxx in leading position, or 111110xx) is invalid, as is a character whose continuation bytes run off the end of the array. An empty array is valid.

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen