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

  • SEARCH
  • Home
  • 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 8416265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:41:59+00:00 2026-06-10T01:41:59+00:00

I have two for loops that I want to write in a function as

  • 0

I have two for loops that I want to write in a function as one. The problem is that it differ only in one instruction

for (int i = 1; i <= fin_cabecera - 1 ; i++ ){
    buffer[i] &= 0xfe;
    if (bitsLetraRestantes < 0) {
        bitsLetraRestantes = 7;
        mask = 0x80;
        letra = sms[++indiceLetra]; //*differs here*
    }
    char c = (letra & mask) >> bitsLetraRestantes--;
    mask >>= 1;
    buffer[i] ^= c; 
}

And the other

for (int i = datos_fichero; i <= tamanio_en_bits + datos_fichero; i++){
    buffer[i] &= 0xfe;
    if (bitsLetraRestantes < 0) {
        bitsLetraRestantes = 7;
        mask = 0x80;
        f.read(&letra, 1);  //*differs here*
    }
    char c = (letra & mask) >> bitsLetraRestantes--;
    mask >>= 1;
    buffer[i] ^= c; 
}

I thought in something like this:

void write_bit_by_bit(unsigned char buffer[], int from, int to, bool type) {
    for (int i = to; i <= from; i++) {
        buffer[i] &= 0xfe;
        if (bitsLetraRestantes < 0) {
            bitsLetraRestantes = 7;
            mask = 0x80;
            type ? (letra = sms[++indiceLetra]) : f.read(&letra, 1);
        }
        char c = (letra & mask) >> bitsLetraRestantes--;
        mask >>= 1;
        buffer[i] ^= c;
    }
}

But I think there has to be a better method.

Context:

I will give more context (I will try explain it as better as I can within my language limitations). I have to read one byte each time because The Buffer variable represents a image pixel. sms is a message that have to be hidden within the image, and letra is a single char of that message. In order to not modify the aspect of the image, each bit of each character have to be written in the last bit of each pixel. Let me give you and example.

letra = 'H' // 01001000 in binary

buffer[0] = 255 // white pixel 11111111

In order to hide the H char, I will need 8 pixel:

The result will be like:

buffer[0] //11111110, 
buffer[1] //11111111 
buffer[2] //11111110 
buffer[3] //11111110 
buffer[4] //11111111 
buffer[5] //11111110 
buffer[6]//11111110 
buffer[7]//11111110

The H is hidden in the last bit of the image. I hope I explained well.

[Solution]

Thanks to @anatolyg I’ve rewrited the code and now works just as I wanted. Here is how it looks:

void write_bit_by_bit(unsigned char buffer[], ifstream& f,int from, int to, char sms[], bool type){

unsigned short int indiceLetra        = 0;
short int bitsLetraRestantes          = 7;
unsigned char mask                    = 0x80; //Empezamos por el bit más significativo (10000000)

char* file_buffer;

if(type){ //Write file data
    int number_of_bytes_to_read = get_file_size(f);
    file_buffer = new char[number_of_bytes_to_read];
    f.read(file_buffer, number_of_bytes_to_read);
}

const char* place_to_get_stuff_from = type ? file_buffer : sms;
char letra = place_to_get_stuff_from[0];

for (int i = from; i <= to; i++) {
    buffer[i] &= 0xfe; //hacemos 0 último bit con máscara 11111110
    //TODO: Hacer con dos for
    if (bitsLetraRestantes < 0) {
        bitsLetraRestantes = 7;
        mask = 0x80;
        letra = place_to_get_stuff_from[++indiceLetra];//letra = sms[++indiceLetra];
    }
    char c = (letra & mask) >> bitsLetraRestantes--;
    mask >>= 1;
    buffer[i] ^= c; //Almacenamos en el ultimo bit del pixel el valor del caracter
}
}

int ocultar(unsigned char buffer[],int tamImage, char sms[], int tamSms){
ifstream f(sms);
if (f) {
    strcpy(sms,basename(sms));
    buffer[0] = 0xff;
    int fin_cabecera = strlen(sms)*8 + 1;
    buffer[fin_cabecera] = 0xff;

    write_bit_by_bit(buffer, f, 1, fin_cabecera -1, sms, WRITE_FILE_NAME);

    int tamanio_en_bits = get_file_size(f) * 8;

    int datos_fichero = fin_cabecera + 1;
    write_bit_by_bit(buffer, f, datos_fichero, tamanio_en_bits + datos_fichero, sms, WRITE_FILE_DATA);

    unsigned char fin_contenido = 0xff;

    short int bitsLetraRestantes          = 7;
    unsigned char mask            = 0x80; 

    for (int i = tamanio_en_bits + datos_fichero + 1;
            i < tamanio_en_bits + datos_fichero + 1 + 8; i++) {
        buffer[i] &= 0xfe;
        char c = (fin_contenido & mask) >> bitsLetraRestantes--;
        mask >>= 1;
        buffer[i] ^= c; 
    }

}
return 0;
}
  • 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-10T01:42:01+00:00Added an answer on June 10, 2026 at 1:42 am

    Since you are talking about optimization here, consider performing the read outside the loop. This will be a major optimization (reading 10 bytes at once must be quicker than reading 1 byte 10 times). This will require an additional buffer for (the file?) f.

    if (!type)
    {
        char f_buffer[ENOUGH_SPACE];
        number = calc_number_of_bytes_to_read();
        f.read(f_buffer, number);
    }
    for (...) {
        // your code
    }
    

    After you have done this, your original question is easy to answer:

    const char* place_to_get_stuff_from = type ? sms : f_buffer;
    for (...) {
            ...
            letra = place_to_get_stuff_from[++indiceLetra];
            ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that iterates through a list, but takes two
I have code that sends web requests through two parallel for each loops. Will
I have a problem i would like parallelize two for loops with openmp. how
I have two divs which should looks like one figure. The problem is with
I want to write a function that returns the nearest next power of 2
Suppose I have two types, TypeA and TypeB, that I want to register with
I'm trying to write a Powershell function that takes an array argument. I want
I have a program that creates pipes between two processes. One process constantly monitors
I have a loop that calculates the similarity between two documents. It collects all
i have two foreach loop to display some data, but i want to use

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.