You ever defined a constant with value `1 << 2`?

In this article, we review a particular way of defining constants found in Ripple codebase. We will look at:
What is Ripple?
constants.js file
What is 1 << 1?
What is Ripple?
Ripple is the elegant TypeScript UI framework, created by Dominic Gannaway, author of lexicaljs and infernojs.
Learn more about Ripple.
constants.js file
You will find the below code in ripple/packages/ripple/…/constants.js file:
export const TEMPLATE_FRAGMENT = 1;
export const TEMPLATE_USE_IMPORT_NODE = 1 << 1;
export const IS_CONTROLLED = 1 << 2;
What I found interesting is the bitwise operator, <<, used.
What is 1 << 1?
This is bitwise flagging using the bitwise left shift operator (<<
)
ChatGPT provided the below explanation:`
1
in binary:
0001
1 << 1
→ shift left by 1:
0010 // which is 2 in decimal
1 << 2
→ shift left by 2:
0100 // which is 4 in decimal
About me:
Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.
Email: ramu.narasinga@gmail.com
Want to learn from open-source? Solve challenges inspired by open-source projects.
References:
Subscribe to my newsletter
Read articles from Ramu Narasinga directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ramu Narasinga
Ramu Narasinga
I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.