# Thread: Bitwise more than 8? or less than 8?

1. ## Bitwise more than 8? or less than 8?

hey all...
i successfully learnt everything in the bitwise lesson and made a program:
Code:
```#include <iostream>
#include <cstdlib>
using namespace std;

char in_use = 0;
int *b = new int;
int *c = new int;

void check(){

for(int a = 0; a <= 8; a++){

if((in_use & (1 << a)) != 0){

cout << "The car " << a + 1 << " is in use...\n";

}else{

cout << "The car " << a + 1 << " is not in use...\n";

}

}

}

void set(int pos){

in_use = (in_use | (1 << pos));
cout << "The car is now in use...";

}

void unset(int pos){

in_use = (in_use & ~(1 << pos));
cout << "The car is now not in use...";

}

int main(){

char x = 'y';
while(x == 'Y' || x == 'y'){

cout << "Welcome to tennisstar's Car Company...";
cout << "\n\n1)Take a Car for rent...";
cout << "\n2)Give a rented car back...";
cout << "\n3)See availability...";
cout << "\n4)Exit...";
cout << "\n\nEnter choice: ";
int *a = new int;
cin >> *a;
switch(*a){

case 1:
cout << "\n===\n\nChoose a car from 1 - 7 which you want to take: ";
cin >> *b;
*b -= 1;
set(*b);
break;
case 2:
cout << "\n===\n\nChoose a car from 1 - 7 which you want to give: ";
cin >> *c;
*c -= 1;
unset(*c);
break;
case 3:
cout << "\n===\n\n";
check();
break;
case 4:
cout << "\n";
system("pause");
return 0;
default:
cout << "\n===\n\nUNKNOWN OPERATION!!!";

}
cout << "\n\n";
system("pause");
system("cls");

}

}```
the program runs successfully..
but my question is: can we use a byte (i.e a char) to store something more than 8 values?
or maybe less?
can we use an encoding in which its 5 bits = 1 byte
or 9 bits = 1 byte

if yes...
how can we do so?

kindly help...
-tennisstar 2. You can use any integral type for bitwise operations. You can use unsigned ints, for example, and on a 64-bit machine you have 64 bits that you can play around with.

To use fewer than 8, or 32, or 64 bits, you can use a bit mask. This acts as a... stencil which doesn't allow you to overstep the boundaries of a pre-designated range. If, as you said, you want to only use 5 bits, you can do the following:
Code:
```const int bitMask = (1 << 5 ) - 1; // 1<<5=100000, (1<<5)-1: 11111
in_use = (in_use | (1<<bit)) & bitMask;```
and if your bit variable was, say, 9, the corresponding bit of the in_use variable doesn't get touched. You can also use some other operators for more convenient bitwise manipulation.
Code:
`in_use |= (1<<bit) & bitMask;` 3. Given that any char type can represent 256 distinct values, it is a fair bit it can represent 8.

Consider how many values can be represented using two bits. The distinct combinations are 00, 01, 10, 11 - i.e. 4 distinct values. With 3 bits, the distinct combinations are 000, 001, 010, 011, 100, 101, 110, 111 - i.e. 8 distinct values. You can keep doig the math, and work out that - with 5 bits - it is possible to represent 32 distinct values. 4. Originally Posted by grumpy Given that any char type can represent 256 distinct values, it is a fair bit it can represent 8.

Consider how many values can be represented using two bits. The distinct combinations are 00, 01, 10, 11 - i.e. 4 distinct values. With 3 bits, the distinct combinations are 000, 001, 010, 011, 100, 101, 110, 111 - i.e. 8 distinct values. You can keep doig the math, and work out that - with 5 bits - it is possible to represent 32 distinct values.
not that...
my question is that:
in my example of the car company which can rent 8 cars
(since 1 char = 1 byte = 8 bits)
how do i make it rent 9 or 10 cars
for that i have to use an encoding where 1 byte = 9 bits or 10 bits respectively...

how to do that? 5. If one char can be used to keep track of whether 8 cars are individually rented out, then 2 chars can be used to keep track of 16 .....

Alternatively, an integral type for which sizeof(type) is 2 or more, can be used ..... 6. thanx a lot grumpy..
that was basic thinkin' which didn;t come to my head while thinking for all complicated stuff  Popular pages Recent additions 