Re: Bitwise Operators....
Quote:
Originally posted by o0obruceleeo0o
...and why they are of any importance.
They are used often for 'flags' in programs, in which a variable in a single byte must store multiple on/off switches (one for each bit).
Also, if you wish to use modulus on an even computer number (2^n,n=integer), then it is the same as using a bitwise AND, if you use the number one less than the modulus. Example:
n % 8 is equal to n & 7
n % 32 is equal to n & 31
There are tons of other reasons.
Bit fields, structure members with specific # of bits
Ok, I read all the post here and i found something i think is VERY usefull and was not mentioned.
By declaring a structure member to be a specific amout of bits,
e.g a char is 8 bit, and int is 16 bit, ect.....
Well, if you have to only save date as a yes or no why not declare a structure member with only one bit. 16 times smaller as an int but you can still either be a 1 bit or a 0 bit. Or what i like to call true or false.
If you used only 2 bits you can say "zero = 00 one = 01, two = 10, and three = 11. You get four posibles with only 2 bits!!!
3 bits can hold 7 values, 4 can hold 15 ect... That also made me realize why computers store info in binary =)
Then your only job is to define answers or values you want each combination to equal.
Example: using people and thier appearance....
Code:
struct appearace {
unsigned hair_color : 3; //defines # of bits
unsigned hieght :2; //lets say tall, ave, short, or NA
unsigned dead :1; //living or dead
};
//then define an array or whatever
struct appearance people[100];
//then assign you values as unsigned int, the equation for how
//many number for you bits size in 0 through 2(#of bits) - 1
people[1].hair_color = 3 //or bits 11
people[1].hieght = 0 // or bit 00, tall
people[1].dead = 1 //they are dead
I hope that helps you understand a great way to use well, i guess not exacty bit operators although im sure you could use them somehow with this. But using bits as a database alone with bit shift could make things pretty simple. Note: I forget if the colon operator is used the same in C++, i think i was using the c stdio.h when doing this. Thanks.
-Thenrkst
Actually yeah think i wrote it wrong??
Lets see....
3 bits, 000, 001, 010, 100, 011, 110, 101, 111...
yep 8 ...=)
Re: Actually yeah think i wrote it wrong??
Quote:
Originally posted by thenrkst
Lets see....
3 bits, 000, 001, 010, 100, 011, 110, 101, 111...
yep 8 ...=)
The formula to calculate this is pretty simple: the number of possibilities is 2^n, where n = number of bits of storage.
So, 8 bits (1 byte) has 2^8 = 256 possibilities.
16 bits = 2^16 = 65,536 possibilities
32 bits = 2^32 = 4,294,967,296 possibilities.
24 bits = 2^24 = 16,777,216 possibilities (this is why you usually see true color mode [24-bit color, 8-bit for each R, G and B] as having 16,000,000 colors).
and so on...