Thread: Good bitshifting tutorial or examples?

  1. #1
    Registered User
    Join Date
    Mar 2005

    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:

    Using a function that the teacher provided:
    void Board::readBoard(const char *filename)
        ifstream input;
        char word[9];;
        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;
    Once the file is read in, if I output board[] directly, it looks like this:
    This is what I'm using to print the board onto the screen, but its coming out backwards:
        // 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';
                    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
    New Zealand
    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
    Join Date
    Oct 2007
    Inside my computer
    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