1. ahhhh!!! They used an example like that in the book "Code", i think I got it now! Thanks a ton!!! By the way, if anyone else is having problems with binary etc, may i suggest picking up the book, Code by Charles Petzoid. It starts out with explaining Morse code and then Brail, and then Binary. I new nothing of what binary was other than a bunch of 1's and 0's and now i understand how it works . It also covers Boolean Algebra!

2. ## 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
};

//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
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

3. >3 bits can hold 7 values, 4 can hold 15 ect

you mean 3 bits can hold 8 values ( 0 - 7), 4 can hold 16 (0- 15)...

4. ## Actually yeah think i wrote it wrong??

Lets see....

3 bits, 000, 001, 010, 100, 011, 110, 101, 111...

yep 8 ...=)

5. ## Just one more thought...

I cant count past 10 in binary without writing it down
That's OK. I have a couple of calculators that do decimal-hex-octal-binary conversion. You can also get a program to do it. (Or write one yourself!) Anybody who does lots of conversion uses a tool! Don't get bogged-down with this shtuff!!! It may be a long time 'till you really need to use it.

Now, people who work with this stuff alot do learn to count to "F" (15)... Well, actually we memorize the 16 conversions between 0 and 15 for binary, hex, and decimal. Then, we can convert between hex and binary of any size! (I tend to forget some of 'em when I don't work with it for awhile.)

Converting to and from decimal is usually done with a calculator or program, although my boss is pretty good at doing it in his head! Again, this is why hex is used... hex-binary is much easier than decimal-binary.

Actually... this may sound weird... but when we work in binary and hex, we usually don't care what the decimal value is... its just a "bit pattern" and all we are doing is bitwise operations... no other math.

EVERYTHING in the computer's memory is stored in binary. The compiler / operating system normally convert to-and-from decimal (or ASCII) for you. Any conversion is done ONLY during input/output. So, you can cin a decimal number and cout the same variable in hex, and the actual variable in memory is NOT affected by the "conversion".

6. Greetings,

Elaborating on thenrkst's ideia, and if speed is not that important you can avoid bitwise operators alltogether:
Code:
```#include <iostream>

typedef unsigned char byte;
typedef unsigned int uint;

typedef union {
struct {
uint bit0:1;
uint bit1:1;
uint bit2:1;
uint bit3:1;
uint bit4:1;
uint bit5:1;
uint bit6:1;
uint bit7:1;
} bits;
byte val;
void display_bits();
void input();
} TBYTE;

void TBYTE::display_bits()
{
std::cout << bits.bit7 << bits.bit6 << bits.bit5 << bits.bit4
<< bits.bit3 << bits.bit2 << bits.bit1 << bits.bit0
<< std::endl;
}

void TBYTE::input()
{
int tmp;
std::cin >> tmp;
val = tmp;
}

int main()
{
TBYTE tbyte;
std::cout << "Byte me: ";
tbyte.input();
std::cout << "Bits in byte: ";
tbyte.display_bits();
// You can check/set/unset the individual bits:
if (tbyte.bits.bit7 == 1) { // same as '(if byte & 128 == 128) byte^=128'
std::cout << "Bit 7 is set. Unsetting: ";
tbyte.bits.bit7 = 0;
tbyte.display_bits();
}
byte b = tbyte.val;
if ((b & 128) == 128) {
b ^= 128;
}
std::cout << int(b) << std::endl;
std::cout << int(tbyte.val) << std::endl;
}```
Makes the code easier to read, but if you want speed stick to the bitwise operators.

7. ## Re: Actually yeah think i wrote it wrong??

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...