Thread: Dealing with bits

  1. #1
    Banned
    Join Date
    Jun 2005
    Posts
    594

    Dealing with bits

    I looked into bitset and search google for information
    on them but i couldnt find anything good, does
    anyone have an information relating to there use,
    that comes with example code.

    What im looking to do, and if it can be done without
    bitset im open to it to, i want to beable to
    set a variable equal by using the binary equivalent.

    Ex:

    Code:
    ???? myByte = 01100001;
    and then how to display it in console such as

    Code:
    cout << myByte << endl;
    which would display 'a' if i choose to display as a character
    or display '97' if i choose to display as a number?

    Using bitset i saw the set() function, but that seems like i have to
    go in and change a bunch of individual bits? For this id like
    to just start with them set where id like them to be.

    If bitset the only way, to do this well, then hwo do i display
    the bitset as a char or int?

    If anyone could help thanks.

    P.S.
    I looked into prelude bit manipulation in the FAQ, but
    its not what im looking for.
    Last edited by ILoveVectors; 07-02-2005 at 06:41 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Something like this?
    Code:
    #include <bitset>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      bitset<8> b ( string ( "01100001" ) );
    
      cout<< b <<'\n'
        << static_cast<char> ( b.to_ulong() ) <<'\n'
        << static_cast<int> ( b.to_ulong() ) <<'\n';
    }
    The only problem with bitset is that it's not dynamic. You have to specify the number of bits when declaring each variable.
    My best code is written with the delete key.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Thank you that is great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  3. Dealing with bits
    By V878 in forum C Programming
    Replies: 12
    Last Post: 10-02-2006, 05:25 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM