samedi 28 août 2021

Why the char in C++ Occupies 7 bits when the length is 1byte i.e is 8 bits

I seen that the char data type is taking only 7 bits of memory to store the character, but in general every where I studied that char occupies 1 byte of memory i.e is 8 bits.

Single character requires 8 bits or 7 bits? if it requires 8 bits means what will be stored in other bit?

#include <iostream>
using namespace std;

int main()
{
    char ch = 'a';
    int val = ch;
    
    while (val > 0)
    {
        (val % 2)? cout<<1<<" " : cout<<0<<" ";
        val /= 2;
    }
    return 0;
}

OP: 1 0 0 0 0 1 1

Aucun commentaire:

Enregistrer un commentaire