Skip to main content

বিটওয়াইজ অপারেটর (Bitwise Operator) — একদম সহজভাবে বুঝে ফেলো

 

বিটওয়াইজ অপারেটর (Bitwise Operator) — একদম সহজভাবে বুঝে ফেলো


প্রোগ্রামিংয়ে আমরা সাধারণত logical operator (যেমন &&, ||, !) ব্যবহার করি শর্ত যাচাই করার জন্য।
কিন্তু bitwise operator একটু আলাদা — এটা কাজ করে bit-level এ, মানে সংখ্যার binary রূপের উপর সরাসরি অপারেশন চালায়।

এই জিনিসটা competitive programming (CP) এ অনেক দরকারি, কারণ এটা দিয়ে আমরা —

  • দ্রুত কিছু গণনা করতে পারি

  • সেট (set) বা mask হিসেবে সংখ্যা ব্যবহার করতে পারি

  • পারফরম্যান্স বাড়াতে পারি

চলো এখন ধাপে ধাপে বুঝে ফেলি। 👇


🧩 ১️⃣  বিটওয়াইজ অপারেটরগুলা কী?

অপারেটর নাম কাজ
& AND দুই bit-ই 1 হলে 1
` ` OR
^ XOR দুই bit আলাদা হলে 1
~ NOT সব bit উল্টে দেয় (1 → 0, 0 → 1)
<< Left Shift bit গুলোকে বামে সরিয়ে দেয়
>> Right Shift bit গুলোকে ডানে সরিয়ে দেয়

🧠 ২️⃣  Bitwise কীভাবে কাজ করে?

ধরা যাক:


Expression Binary Result Decimal Result Explanation
a & b 0001 1 দুই জায়গায়ই 1 থাকলে 1
`a b` 0111 7
a ^ b 0110 6 ভিন্ন হলে 1
~a 1010 -6 1 ↔ 0 উল্টে যায় (Two’s complement)
a << 1 1010 10 সব bit এক ধাপ বামে (×2)
a >> 1 0010 2 সব bit এক ধাপ ডানে (÷2)

💡 ৩️⃣  Bitwise vs Logical Operator

ধরন উদাহরণ কাজের ধরণ ফলাফল
Logical &&, ` , !`
Bitwise &, ` , ^, ~` bit-level এ কাজ করে

🔹 উদাহরণ:


দুইটাই দেখতে একইরকম মনে হলেও কাজ পুরো আলাদা।


🧰 ৪️⃣  বাস্তব জীবনের উদাহরণ: Permissions System 🎯

ধরো তুমি একটা সিস্টেম বানাচ্ছো যেখানে প্রতিটি user এর permission (যেমন read, write, execute) binary আকারে সংরক্ষণ করা হয়।

Permission Binary Decimal
Read 001 1
Write 010 2
Execute 100 4

এখন:


👉 এখন user এর permission: 7 (111)

এখন যদি চেক করতে চাও user এর write permission আছে কিনা:


✅ Output: Has Write Access

অর্থাৎ bitwise operator দিয়ে permission সিস্টেম, feature flags, mask, toggle — এসব খুব দ্রুত করা যায়।


⚡ ৫️⃣  Competitive Programming এ ব্যবহার

  1. Subset Enumeration (Masking):

    for (int mask = 0; mask < (1 << n); mask++) {
        // subset তৈরি
    }
    
  2. Check i-th bit is ON/OFF:

    if (mask & (1 << i)) cout << "Bit " << i << " is ON\n";
    
  3. Turn ON/OFF bits:

    mask |= (1 << i);  // Turn ON
    mask &= ~(1 << i); // Turn OFF
    
  4. Count bits:

    __builtin_popcount(mask); // কতগুলো bit 1 আছে
Example:



🎯 সারসংক্ষেপ

  • Bitwise অপারেটর মানে হলো সংখ্যা কে bit ধরে কাজ করা।

  • এটা দ্রুত, powerful, আর অনেক hidden trick solve করতে সাহায্য করে।

  • CP-তে subset, mask, optimization, permission system — এসব ক্ষেত্রে খুব দরকারি।

Comments

Popular posts from this blog

Top 5 Free Background Remover Tools for Effortless Image Editing.

 The Best Free Image Background Remover Online Tools Removing the background from an image can be a tedious and time-consuming task, especially if you don't have access to professional software like Photoshop. Fortunately, there are several free online tools that can help you remove the background from an image quickly and easily. In this blog post, we'll explore the best free image background remover online tools and how they can help you create stunning visuals for your projects. Remove.bg Remove.bg is a popular online tool that uses AI technology to automatically remove the background from an image. All you need to do is upload your image, and the tool will do the rest. Remove.bg is free to use for images up to 0.25 megapixels and offers paid plans for larger images. Clipping Magic Clipping Magic is a user-friendly online tool that allows you to remove the background from an image in just a few clicks. The tool uses AI technology to accurately detect the foreground and backg...

AI is not scary: Why we should embrace artificial intelligence

  AI is not scary: Why we should embrace artificial intelligence Artificial intelligence (AI) is a term that refers to the ability of machines to perform tasks that normally require human intelligence, such as reasoning, learning, decision making, and creativity. AI is often seen as a threat to human jobs, privacy, and even existence, but in this blog post, I will argue that AI is not scary, but rather a powerful tool that can enhance our lives and society in many ways. First of all, AI can help us solve some of the most pressing problems that humanity faces, such as climate change, poverty, disease, and education. AI can analyze massive amounts of data, find patterns and insights, and propose solutions that humans may not be able to discover or implement. For example, AI can help us optimize energy consumption, reduce greenhouse gas emissions, predict natural disasters, diagnose diseases, design drugs and vaccines, and personalize learning. Secondly, AI can augment our capabilitie...

Discovering Artificial Intelligence, A Beginner's Guide

Discovering Artificial Intelligence: A Beginner's Guide Artificial Intelligence (AI) is one of the most exciting and rapidly developing fields of technology today, and if you're interested in learning about it, there has never been a better time to get started. Here's a beginner's guide to discovering AI. S tart with the basics: Before you dive into the complexities of AI, it's important to have a solid understanding of its foundations. Start by learning about the different types of AI, including machine learning, deep learning, and natural language processing. Read books and articles: There is a wealth of information available on AI, and reading books and articles is a great way to gain a deeper understanding of the topic. Popular books on AI include "Artificial Intelligence: A Modern Approach" by Stuart Russell and Peter Norvig, and "Deep Learning" by Ian Goodfellow, Yoshua Bengio, and Aaron Courville. Participate in online courses: Onli...