Thread: Bitwise Operators....

  1. #16
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    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. #17
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    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
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  3. #18
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >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. #19
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Actually yeah think i wrote it wrong??

    Lets see....

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

    yep 8 ...=)
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  5. #20
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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. #21
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    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. #22
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM