binary, bitwise etc

Started by
16 comments, last by nomichi 17 years, 11 months ago
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?
Regards,nomichi
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.
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.
Also, check out the Bitwise Operators Wikipedia entry.
thank you all. I will check those links out.
Regards,nomichi
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 :)
Regards,nomichi
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.
  • 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......
    ______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
    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. :)
    Regards,nomichi
    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 :)
    Regards,nomichi

    This topic is closed to new replies.

    Advertisement