Gowtham
PRO
last year
Gowthamcountry asked

How are bits calculated?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Gowtham,

Good question! Let’s break it down simply.

A bit is the smallest unit of data in a computer. It's either a 0 or a 1.

But in practice, we usually deal with bytes, and 1 byte = 8 bits.

So when we talk about how many bits something uses, we’re really asking how much space it takes in memory. For example:

  • char = 1 byte = 8 bits

  • int = 4 bytes = 32 bits

  • double = 8 bytes = 64 bits

You can check this in C++ using the sizeof operator:

#include 
using namespace std;

int main() {
    cout << "char: " << sizeof(char) << " byte" << endl;
    cout << "int: " << sizeof(int) << " bytes" << endl;
    cout << "double: " << sizeof(double) << " bytes" << endl;
    return 0;
}

The output tells you how many bytes each type uses. Just multiply that by 8 to get the number of bits.

So bits are calculated based on the data type and how much space it uses in memory.

If you’d like help with binary values or how bits are used in operations, I’m here to help.

C++
This question was asked as part of the Learn C++ Basics course.