DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Originally published at thinkthroo.com

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:

  1. What is Ripple?

  2. constants.js file

  3. 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;
Enter fullscreen mode Exit fullscreen mode

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.

References:

  1. https://github.com/trueadm/ripple/blob/main/packages/ripple/src/constants.js

  2. https://github.com/trueadm/ripple/tree/main

  3. https://github.com/trueadm

Top comments (1)

Collapse
 
fyodorio profile image
Fyodor

It still leaves the question "Why?!" though 😅