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:
javascript
0001
-
1 << 1
→ shift left by 1:
javascript
0010 // which is 2 in decimal
-
1 << 2
→ shift left by 2:
javascript
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.
Top comments (1)
It still leaves the question "Why?!" though 😅