JavaScript Bitwise Operators & BigInt: সম্পূর্ণ গাইড

JavaScript Bitwise Operators এবং BigInt সহজ উদাহরণসহ শিখুন। AND, OR, XOR, NOT, Shift Operators ও BigInt ব্যবহার বিস্তারিত জানুন।

JavaScript Bitwise Operators এবং BigInt কী?

JavaScript Bitwise Operators number-এর binary bits-এর উপর কাজ করে। এগুলো ব্যবহার করে bit level-এ AND, OR, XOR, NOT এবং bit shift-এর মতো operation করা যায়। অন্যদিকে BigInt ব্যবহার করা হয় JavaScript-এর সাধারণ Number-এর safe integer range-এর চেয়ে বড় পূর্ণসংখ্যা নিয়ে কাজ করার জন্য।

এই article-এ JavaScript-এর সব গুরুত্বপূর্ণ Bitwise Operators এবং BigInt সহজ উদাহরণসহ আলোচনা করা হবে।

JavaScript Bitwise Operators

Bitwise operator কোনো number-এর binary representation-এর প্রতিটি bit-এর উপর operation করে। JavaScript-এর bitwise operators সাধারণত number-কে 32-bit signed integer হিসেবে process করে।

JavaScript-এর Bitwise Operators List

&    Bitwise AND
|    Bitwise OR
^    Bitwise XOR
~    Bitwise NOT
<<   Left Shift
>>   Signed Right Shift
>>>  Unsigned Right Shift

Binary Number কী?

Bitwise operator বোঝার আগে binary সম্পর্কে basic ধারণা থাকা ভালো। Computer internally data-এর bits নিয়ে কাজ করে। একটি bit-এর value হতে পারে 0 অথবা 1

যেমন decimal number 5-এর binary representation হলো:

5 = 0101

আর decimal number 3:

3 = 0011

1. Bitwise AND (&)

Bitwise AND operator হলো &। দুইটি bit-ই 1 হলে result হবে 1। অন্য যেকোনো ক্ষেত্রে result হবে 0।

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

JavaScript example:

let x = 5;
let y = 3;

console.log(x & y);

Binary:

5 = 0101
3 = 0011

    0101
  & 0011
  --------
    0001

0001 = 1

তাই output:

1

2. Bitwise OR (|)

Bitwise OR operator হলো |। দুইটি bit-এর মধ্যে অন্তত একটি 1 হলে result 1 হবে।

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

Example:

let x = 5;
let y = 3;

console.log(x | y);

Binary calculation:

5 = 0101
3 = 0011

    0101
  | 0011
  --------
    0111

0111 = 7

Output:

7

3. Bitwise XOR (^)

Bitwise XOR operator হলো ^। দুইটি bit আলাদা হলে result 1 হবে। আর একই হলে result 0 হবে।

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

Example:

let x = 5;
let y = 3;

console.log(x ^ y);

Binary:

5 = 0101
3 = 0011

    0101
  ^ 0011
  --------
    0110

0110 = 6

Output:

6

4. Bitwise NOT (~)

Bitwise NOT operator হলো ~। এটি প্রতিটি bit উল্টে দেয়। অর্থাৎ 1 হলে 0 এবং 0 হলে 1 হয়।

~1 = 0
~0 = 1

JavaScript-এ:

let x = 5;

console.log(~x);

Output:

-6

এর কারণ JavaScript-এর signed integer representation-এ bitwise NOT-এর ক্ষেত্রে একটি গুরুত্বপূর্ণ নিয়ম হলো:

~x = -(x + 1)

তাই:

~5
= -(5 + 1)
= -6

5. Left Shift (<<)

Left Shift operator হলো <<। এটি binary bits-কে বাম দিকে সরিয়ে দেয় এবং ডান পাশে 0 যোগ করে।

let x = 5;

console.log(x << 1);

Binary:

5 = 0101

0101 << 1
= 1010

1010 = 10

Output:

10

সহজভাবে, positive integer-এর ক্ষেত্রে:

x << 1 ≈ x × 2
x << 2 ≈ x × 4
x << 3 ≈ x × 8

6. Signed Right Shift (>>)

Signed Right Shift operator হলো >>। এটি bits-কে ডান দিকে সরায় এবং sign bit ধরে রাখে।

let x = 20;

console.log(x >> 1);

Output:

10

আর:

20 >> 2
= 5

Positive number-এর ক্ষেত্রে এটি সাধারণভাবে division by powers of 2-এর মতো কাজ করে, তবে bitwise conversion-এর কারণে সব ক্ষেত্রে সাধারণ arithmetic division-এর সমান নয়।

7. Unsigned Right Shift (>>>)

Unsigned Right Shift operator হলো >>>। এটি bits-কে ডান দিকে সরিয়ে দেয় এবং বাম পাশে 0 যোগ করে। এটি result-কে unsigned 32-bit integer হিসেবে treat করে।

let x = 20;

console.log(x >>> 2);

Output:

5

Negative number-এর ক্ষেত্রে >> এবং >>>-এর পার্থক্য বিশেষভাবে দেখা যায়।

Bitwise Operators-এর Truth Table

A B A & B A | B A ^ B
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Bitwise Operator-এর Practical Example

Bitwise operator permissions বা flags-এর মতো data represent করতে ব্যবহার করা যেতে পারে।

const READ = 1;
const WRITE = 2;
const DELETE = 4;

let permission = READ | WRITE;

console.log(permission);

এখানে READ এবং WRITE permission একসাথে রাখা হয়েছে। পরে AND ব্যবহার করে কোনো permission আছে কি না পরীক্ষা করা যায়।

if (permission & READ) {
  console.log("Read permission আছে");
}

JavaScript BigInt কী?

BigInt হলো JavaScript-এর একটি primitive data type, যা খুব বড় পূর্ণসংখ্যা বা integer নিয়ে কাজ করার জন্য ব্যবহার করা হয়। সাধারণ JavaScript Number type-এর safe integer limit-এর বাইরে বড় integer নিয়ে exact calculation প্রয়োজন হলে BigInt ব্যবহার করা যায়।

BigInt তৈরি করার সবচেয়ে সহজ উপায় হলো number-এর শেষে n যোগ করা।

let bigNumber = 123456789012345678901234567890n;

console.log(bigNumber);

BigInt কেন প্রয়োজন?

JavaScript-এর সাধারণ Number type নিরাপদভাবে integer represent করতে পারে:

Number.MAX_SAFE_INTEGER

এর value হলো:

9007199254740991

এর চেয়ে বড় integer-এর ক্ষেত্রে সাধারণ Number ব্যবহার করলে exact integer precision হারানোর সম্ভাবনা থাকে।

let x = 9007199254740992;

console.log(x);

এই ধরনের বড় integer-এর exact calculation প্রয়োজন হলে BigInt ব্যবহার করা নিরাপদ।

let x = 9007199254740992n;

console.log(x);

BigInt তৈরি করার নিয়ম

BigInt Literal

Number-এর শেষে n লিখলে BigInt literal তৈরি হয়।

let x = 100n;
let y = 500000000000000000000n;

console.log(x);
console.log(y);

BigInt() Constructor

String বা integer value থেকে BigInt() ব্যবহার করেও BigInt তৈরি করা যায়।

let x = BigInt("12345678901234567890");

console.log(x);

আর:

let x = BigInt(100);

console.log(x);

Output হবে BigInt value:

100n

BigInt-এর Arithmetic Operators

BigInt-এর সাথে সাধারণ arithmetic operators ব্যবহার করা যায়।

let x = 100n;
let y = 20n;

console.log(x + y); // 120n
console.log(x - y); // 80n
console.log(x * y); // 2000n
console.log(x / y); // 5n
console.log(x % y); // 0n
console.log(x ** 2n); // 10000n

BigInt division-এ decimal অংশ রাখা হয় না।

console.log(10n / 3n);

Output:

3n

BigInt এবং Number একসাথে Arithmetic করা যায় না

একটি গুরুত্বপূর্ণ নিয়ম হলো BigInt এবং Number সরাসরি arithmetic operation-এর সাথে ব্যবহার করা যায় না

let x = 10n;
let y = 5;

console.log(x + y);

এখানে error হবে, কারণ একটি BigInt এবং অন্যটি Number।

দুটিকে একই type-এ convert করতে হবে।

let x = 10n;
let y = 5n;

console.log(x + y);

অথবা Number-কে BigInt করা যায়:

let x = 10n;
let y = BigInt(5);

console.log(x + y);

BigInt Comparison

BigInt এবং Number-এর মধ্যে comparison করা যায়।

console.log(10n === 10); // false
console.log(10n == 10);  // true
console.log(10n > 5);    // true
console.log(10n < 20);   // true

10n === 10 false হওয়ার কারণ হলো দুটির data type আলাদা।

10n → bigint
10  → number

BigInt-এর Type Check

typeof ব্যবহার করে BigInt-এর type জানা যায়।

let x = 100n;

console.log(typeof x);

Output:

bigint

BigInt-এর Bitwise Operators

BigInt-এর সাথেও কিছু bitwise operators ব্যবহার করা যায়।

let x = 5n;
let y = 3n;

console.log(x & y); // 1n
console.log(x | y); // 7n
console.log(x ^ y); // 6n

BigInt-এর ক্ষেত্রে bitwise operation করলে result-ও BigInt হয়।

BigInt এবং Bitwise NOT

BigInt-এর সাথেও ~ ব্যবহার করা যায়।

let x = 5n;

console.log(~x);

Output:

-6n

BigInt Shift Operators

BigInt-এর সাথে left shift এবং signed right shift ব্যবহার করা যায়।

let x = 5n;

console.log(x << 1n); // 10n
console.log(x >> 1n); // 2n

তবে BigInt-এর ক্ষেত্রে shift count-ও BigInt হতে হয়।

BigInt-এর সাথে Math ব্যবহার

JavaScript-এর সাধারণ Math methods যেমন Math.sqrt(), Math.max() ইত্যাদি সরাসরি BigInt-এর জন্য ব্যবহার করা যায় না।

let x = 100n;

// Math.sqrt(x); // Error

BigInt এবং Number-এর জন্য আলাদা handling প্রয়োজন।

BigInt থেকে Number Conversion

Number() ব্যবহার করে BigInt-কে Number-এ convert করা যায়।

let x = 100n;

let y = Number(x);

console.log(y);
console.log(typeof y);

Output:

100
number

তবে খুব বড় BigInt-কে Number-এ convert করলে precision loss হতে পারে।

Number থেকে BigInt Conversion

Safe integer range-এর মধ্যে থাকা integer Number-কে BigInt-এ convert করা যায়।

let x = 100;

let y = BigInt(x);

console.log(y);

Output:

100n

Decimal number-কে সরাসরি BigInt করা যায় না।

let x = 10.5;

// BigInt(x); // Error

BigInt-এর সাথে Decimal ব্যবহার করা যায় না

BigInt শুধুমাত্র integer-এর জন্য। এতে decimal value রাখা যায় না।

let x = 10n;

// 10.5n; // Invalid

BigInt ব্যবহার করার সময় গুরুত্বপূর্ণ নিয়ম

  1. BigInt literal-এর শেষে n ব্যবহার করুন।
  2. BigInt এবং Number-এর মধ্যে সরাসরি arithmetic operation করবেন না।
  3. Decimal value-এর জন্য BigInt ব্যবহার করবেন না।
  4. বড় exact integer calculation-এর জন্য BigInt ব্যবহার করুন।
  5. BigInt-কে Number-এ convert করার সময় precision loss-এর বিষয়টি মনে রাখুন।
  6. BigInt-এর type জানতে typeof ব্যবহার করুন।

Bitwise Operators বনাম BigInt

বিষয় Bitwise Operators BigInt
মূল উদ্দেশ্য Bits-এর উপর operation বড় integer নিয়ে কাজ
Example 5 & 3 123456789n
Data Type সাধারণত Number BigInt
Binary operation হ্যাঁ কিছু bitwise operation সমর্থন করে
Decimal Number conversion-এর মাধ্যমে process হয় সমর্থন করে না

Complete Bitwise Example

let a = 5;
let b = 3;

console.log("AND:", a & b);
console.log("OR:", a | b);
console.log("XOR:", a ^ b);
console.log("NOT:", ~a);
console.log("Left Shift:", a << 1);
console.log("Right Shift:", a >> 1);
console.log("Unsigned Right Shift:", a >>> 1);

Complete BigInt Example

let a = 12345678901234567890n;
let b = 10n;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);

console.log(typeof a);

JavaScript Bitwise Operators এবং BigInt শেখার Steps

  1. প্রথমে decimal এবং binary number সম্পর্কে basic ধারণা নিন।
  2. & Bitwise AND practice করুন।
  3. | Bitwise OR practice করুন।
  4. ^ Bitwise XOR practice করুন।
  5. ~ Bitwise NOT-এর formula বুঝুন।
  6. << Left Shift practice করুন।
  7. >> Right Shift এবং >>> Unsigned Right Shift-এর পার্থক্য বুঝুন।
  8. এরপর Number.MAX_SAFE_INTEGER সম্পর্কে জানুন।
  9. BigInt দিয়ে বড় integer তৈরি করুন।
  10. BigInt-এর সাথে arithmetic এবং comparison practice করুন।
  11. BigInt এবং Number একসাথে arithmetic করা যায় না—এই নিয়মটি মনে রাখুন।

উপসংহার

JavaScript Bitwise Operators binary bit level operation-এর জন্য গুরুত্বপূর্ণ। এগুলোর মধ্যে &, |, ^, ~, <<, >> এবং >>> সবচেয়ে গুরুত্বপূর্ণ। অন্যদিকে BigInt ব্যবহার করা হয় খুব বড় পূর্ণসংখ্যা exactভাবে handle করার জন্য। JavaScript-এর advanced programming, low-level data manipulation, permissions, flags, algorithms এবং large integer calculations বুঝতে এই বিষয়গুলো জানা উপকারী।

Post a Comment

To avoid SPAM, all comments will be moderated before being displayed.
Don't share any personal or sensitive information.