Public Group

binary, bitwise etc

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

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 on other sites
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 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 on other sites
Also, check out the Bitwise Operators Wikipedia entry.

Share on other sites
thank you all. I will check those links out.

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 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 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 on other sites
Quote:
 Original post by alniteHere'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 secondThe next 6 bit is the minuteThe last 4 bit is the hour in 12-hour format hour second | | +--+ +-----+ | | | | XXXX XXXX XXXX XXXX | | +-----+ | minuteExtract 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 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 :)

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• Forum Statistics

• Total Topics
633313
• Total Posts
3011321
• Who's Online (See full list)

There are no registered users currently online

×