Like Tree1Likes
  • 1 Post By twomers

Bitwise more than 8? or less than 8?

This is a discussion on Bitwise more than 8? or less than 8? within the C++ Programming forums, part of the General Programming Boards category; hey all... i successfully learnt everything in the bitwise lesson and made a program: Code: #include <iostream> #include <cstdlib> using ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    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. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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;
    Lesshardtofind likes this.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,833
    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.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Quote Originally Posted by grumpy View Post
    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. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,833
    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 .....
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    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 subscribe to a feed

Similar Threads

  1. Bitwise...
    By tennisstar in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2012, 04:34 AM
  2. Bitwise And
    By Alan Gott in forum C Programming
    Replies: 21
    Last Post: 11-22-2011, 02:09 PM
  3. Help with bitwise AND
    By tehjojo in forum C Programming
    Replies: 1
    Last Post: 04-03-2010, 02:23 PM
  4. Bitwise 'and' vs '=='
    By Ducky in forum Windows Programming
    Replies: 4
    Last Post: 07-27-2009, 12:37 PM
  5. bitwise help
    By linuxdude in forum C Programming
    Replies: 8
    Last Post: 11-20-2003, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21