I wrote the following a while ago, it provides a minor mode highlight-c-bits
that highlights exactly your pattern. It only highlights the 1:s, but it should be trivial to extend to highlighting both 0:s and 1:s.
Technically, it first search for 0b
then use a subsearch using an anchored rule to find and highlight 1
:s.
;;;###autoload
(define-minor-mode highlight-c-bits-mode
"Minor mode that highlights set bits in C-style 0b... binary constants."
:group 'highlight-c-bits
(if highlight-c-bits-mode
(highlight-c-bits-add-keywords)
(highlight-c-bits-remove-keywords))
;; As of Emacs 24.4, `font-lock-fontify-buffer' is not legal to
;; call, instead `font-lock-flush' should be used.
(if (fboundp 'font-lock-flush)
(font-lock-flush)
(when font-lock-mode
(with-no-warnings
(font-lock-fontify-buffer)))))
;;;###autoload
(define-global-minor-mode highlight-c-bits-global-mode highlight-c-bits-mode
(lambda ()
(when (apply 'derived-mode-p highlight-c-bits-modes)
(highlight-c-bits-mode 1)))
:group 'highlight-c-bits)
(defvar highlight-c-bits-keywords
'(("\\_<0b\\([01']+\\)\\_>"
("1"
;; PRE-MATCH-FORM:
(progn
(goto-char (match-beginning 0))
(match-end 0))
;; POST-MATCH-FORM:
(goto-char (match-end 1))
(0 'font-lock-warning-face nil prepend)))))
(defun highlight-c-bits-add-keywords ()
"Add font-lock keywords to highlight C-style 0b... binary constants."
(font-lock-add-keywords
nil
highlight-c-bits-keywords
'append))
(defun highlight-c-bits-remove-keywords ()
"Remove font-lock keywords for highlighting C-style 0b... binary constants."
(font-lock-remove-keywords nil highlight-c-bits-keywords))