Thread: Good bitshifting tutorial or examples?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Good bitshifting tutorial or examples?

    I need to take an array of characters (8 total) and output each character as its binary number using bit shifting. The idea is to print out an 8x8 grid using a single character for each row. I read in the "level.dat" file, which looks like this:

    Code:
    00010010
    11101110
    01010010
    01000010
    11111011
    00110010
    01100010
    01000010
    Using a function that the teacher provided:
    Code:
    void Board::readBoard(const char *filename)
    {
        ifstream input;
        char word[9];
        input.open(filename);
        for (int i = 0; i < BOARD_SIZE; i++)
        {
            input >> word;
            for (int j = 0; j < 8; j++)
            {
                board[i] |= word[j] & 0x01;
                if (j != 7)
                {
                    board[i] <<= 1;
                }
            }
        }
        input.close();
    }
    Once the file is read in, if I output board[] directly, it looks like this:
    Code:
    ↕
    ε
    R
    B
    √
    2
    b
    B
    This is what I'm using to print the board onto the screen, but its coming out backwards:
    Code:
        // Prints all 8 rows backwards, argh!
        for (int i = 0; i < 8; i++)
        {
            string bit = "";
    
            for (int j = 0; j < 8; j++)
            {
                if((board[i] & 1 ) == 0)
                    bit += '0';
                else
                    bit += '1';
    
                board[i] >>= 1;
            }
    
            cout << i << line << bit << endl;
        }
    I've spent about 5 hrs on Google, coding, trying things out and can't get anything to really work. In this case I'm just building a string and outputting it, I'm not really doing anything worth while. My next step after I get the "level" to display is to do bitwise operations on the rows and columns to make a game that you use to blank out all the 1's to 0's.

    It almost feels like I should make another function that returns the binary number a given value and then loop through that so I can do comparisons in my other functions. Sort of just thinking out loud now. Anyone have any advice, or know of any good pages that talk about bit shifting or how to take a char to a binary number using bits? This topic is super confusing and I'm having a heck of a time wrapping my head around bits!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Do you realy mean for the code that converts the board into a binary string to be destructive? (I mean the board ends up as zeroes)

    There are many easy ways to correct this, if your only problem is that it comes out backwards. You can prepend onto the string instead, or you can move the bits in the other direction, testing the highest bit instead of the lowest. Have a go at either of those and post your updated code.

    Nobody spends 5 hours on google searching for something, that's just ridiculous. If you want people to feel sorry for you, then at least be realistic.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just so you know, you are printing it backwards (and destroying your board in the process). The MSB is printed first (because that's how we write it), but you are printing LSB first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what are some good C++ source code examples?
    By orion- in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2005, 09:24 PM
  2. Where is there a good SDL tutorial?
    By ballmonkey in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:43 PM
  3. Tutorial any good?
    By Anthony172 in forum C++ Programming
    Replies: 10
    Last Post: 10-22-2002, 01:32 PM
  4. FAQ: Examples of good questions (General)
    By Prelude in forum FAQ Board
    Replies: 1
    Last Post: 10-11-2002, 08:57 PM
  5. Looking for a good C++ TCP/IP tutorial.
    By OmegaFirebolt in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2002, 09:47 PM