Jump to content
  • Advertisement
Sign in to follow this  
nomichi

binary, bitwise etc

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know any good tutorials on binary and the bitwise operators. I've been seeing more and more of it being used in example code so I think I should practice using these things. I'm not really sure when/why to use this stuff yet so I want to learn more about it in general. Anyone point me to a good resource?

Share this post


Link to post
Share on other sites
Advertisement
The Wikipedia entry for the binary number system seems like a good reference. Plus there's a bunch of links to the bitwise operators around the center of the page. You'll also want to read up on Hexadecimal since most programming languages only allow decimal or hex.

Share this post


Link to post
Share on other sites
When you see the word 'bitwise', that means the operation is carried out for each bit. For example, given A and B of length n bits, a bitwise operator X works as follow:

A X B = A(0) X B(0), A(1) X B(1), ... , A(n-1) X B(n-1)

That is, for each bit in A and B, performs X.

Share this post


Link to post
Share on other sites
well I've done some reading and I think I'm ready to try some exercises. I'm gonna google around and try to find some. If anyone knows any good exercises I should try please post a link. If you wanna make some up that's fine too. Thanks :)

Share this post


Link to post
Share on other sites
Here's a couple of exercises from me:

  • A library function, lib_gettime(), returns a 16-bit integer that indicates the current time in the following format:
    Starting from the least significat bit,
    The first 6 bit is the second
    The next 6 bit is the minute
    The last 4 bit is the hour in 12-hour format

    hour second
    | |
    +--+ +-----+
    | | | |
    XXXX XXXX XXXX XXXX
    | |
    +-----+
    |
    minute

    Extract second, minute, and hour from this 16-bit number into three separate numbers.

  • RGBA colors contain four components red, green, blue, and alpha. You are given the components in four 8-bit numbers each of which ranged from 0-255. Using bitwise operators, combine these four numbers into one 32-bit number. (a) The order is: R-G-B-A. (b) B-G-R-A.

    Share this post


    Link to post
    Share on other sites
    Also, if you're using C++, std::bitset is a handy container for working with binary values. It lets you output in binary in a straightforward manner, unlike trying to output an int in binary format......

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by alnite
    Here's a couple of exercises from me:

  • A library function, lib_gettime(), returns a 16-bit integer that indicates the current time in the following format:
    Starting from the least significat bit,
    The first 6 bit is the second
    The next 6 bit is the minute
    The last 4 bit is the hour in 12-hour format

    hour second
    | |
    +--+ +-----+
    | | | |
    XXXX XXXX XXXX XXXX
    | |
    +-----+
    |
    minute

    Extract second, minute, and hour from this 16-bit number into three separate numbers.

  • RGBA colors contain four components red, green, blue, and alpha. You are given the components in four 8-bit numbers each of which ranged from 0-255. Using bitwise operators, combine these four numbers into one 32-bit number. (a) The order is: R-G-B-A. (b) B-G-R-A.


  • Thanks. I just popped on to check the forum. I'm gonna try these tomorrow when I have some spare time. :)

    Share this post


    Link to post
    Share on other sites
    Well here is what I came up with for the first exercise from alnite.


    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>

    using namespace std;

    int lib_getTime( void )
    {
    int hour = 1 + (rand() % 12);
    int min = rand() % 60;
    int sec = rand() % 60;

    return ( (hour << 12) | (min << 6) | sec );
    }

    void displayBinTime( int& t )
    {
    cout << "Time in binary: ";
    for ( int i = 15; i >= 0; i-- )
    {
    if ( t & (1 << i ) )
    cout << '1';
    else
    cout << '0';

    if ( i != 0 && i % 4 == 0 )
    cout << ':';
    }

    cout << endl;
    }

    int main()
    {
    srand( time(0) ); // seed random number generator

    int time = lib_getTime();
    int hour = time >> 12;
    int min = time >> 6 & 0x3F;
    int sec = time & 0x3F;

    cout << "Time in decimal: ";
    cout << setw(2) << setfill('0') << hour << ':'
    << setw(2) << setfill('0') << min << ':'
    << setw(2) << setfill('0') << sec << endl;

    displayBinTime(time);

    return 0;
    }



    Seems to work. I'm gonna check out the next one :)

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!