Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:22:29+00:00 2026-06-02T22:22:29+00:00

Trying to print out the bits of each char stored in an array. I’ve

  • 0

Trying to print out the bits of each char stored in an array. I’ve looked up some code and tried a version to suit my needs. Problem is that I only seem to be getting the first char in the array.

//read_buffer is the array I want to iterate through, bytes_to_read is the number of 
//index positions I want to_read. (array is statically allocated and filled using read()
//funct, therefore there are some garbage bits after the char's I want), bytes_to_read
//is what's returned from read() and how many bytes were actually read into array
void PrintBits(char read_buffer[], int bytes_to_read)
{

        int bit = 0;
        int i = 0;
        char char_to_print;

        printf("bytes to read: %d\n", bytes_to_read); //DEBUG

        for (; i < bytes_to_read; i++)
        {
                char_to_print = read_buffer[i];

                for (; bit < 8; bit++)
                {
                        printf("%i", char_to_print & 0X01);
                        char_to_print >> 1;
                }
                printf(" ");
                printf("bytes_to_read: %d -- i: %d", bytes_to_read, i);
        }

        printf("\n");
}

Basically what I’m getting is: 00000000 Not sure why this is. Through debugging I’ve found it only to be printing the first bit and nothing else. I’ve also proven that the outer loop is actually iterating through int’s 0 – 29… So it should be iterating through the char’s in the array. I’m stumped.

Also, can someone tell me what the & 0x01 is doing in the printf statement. I found that in someone else’s code and I am unsure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:22:31+00:00Added an answer on June 2, 2026 at 10:22 pm

    You missed that

                       char_to_print >>= 1;
    

    char_to_print was not shifted AND saved

    And you should initialize bit each time with an new char_to_print

                for (bit = 0; bit < 8; bit++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to print out the names of each person in the loop.
I am trying to print out documents in various different orders. Right now I
I'm using Django templating with Google App Engine. I'm trying unsuccessfully to print out
I'm trying to come up with an algorithm that will print out all possible
I'm trying to write a very simple program, I want to print out the
I'm trying to iterate through all nodes, so I can print them out for
I'm trying to figure out how to get text to print sideways in a
When an exe file is run it prints out some stuff. I'm trying to
I'm currently trying to print out a history of movements for players in a
I'm trying to convert a 128-bit unsigned integer stored as an array of 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.