-1
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

const int BITS_IN_BYTE = 8;

 

int main(void)
{
    string text = get_string("Text to encrypt!: ");
    int bit;
    int count = 1;
    for (int i = 0, y = 7, n = strlen(text); i < n; i++) {
        int result = text[i];
        while (result != 0 && y >= 0) {
            if (result > (2^y)) {
                bit = 1;
                print_bulb(bit);
                result = text[i] - (2^y);
                y--;
            }
            else {
                bit = 0;
                print_bulb(bit);
                y--;
                }
        printf("\n");
        }
    }
 }

void print_bulb(int bit)
{
    if (bit == 0)
    {
        // Dark emoji
        printf("\U000026AB");
    }
    else if (bit == 1)
    {
        // Light emoji
        printf("\U0001F7E1");
    }
}

I tried this for the cs50 course (https://cs50.harvard.edu/x/2023/psets/2/bulbs/) and Im not sure why it doesnt work correctly. The program's purpose is to get a string from the user and display it in binary using "light bulbs". I looked up now that I was supposed to do it with remainders, but I really want to know what went wrong with this one. I wonder why all bulbs are still "on", either though the value of the array item is supposed to be going down.

Rae
  • 1
  • 2
  • 4
    At an immediate glance, `2^y` looks pretty concerning. `^` in C means XOR and not "raised to the power of", if that's what you were trying to do instead. That being said, I know noting about the real purpose of the program and I'd suggest you put a brief description for those not familiar with CS50. – mediocrevegetable1 Jul 06 '23 at 17:14
  • ...is better coded as `(1 << y)`. If you are "supposed to do it with remainders" I would expect to see the `%` operator in the code. – Weather Vane Jul 06 '23 at 17:16
  • Note that `const int BITS_IN_BYTE = 8;` is better just as [`CHAR_BIT`](https://stackoverflow.com/a/3200969/4142924). Also by setting `y = 7` you seem to have ignored the definition you made. – Weather Vane Jul 06 '23 at 17:22
  • Your code only prints the sequence for _one_ input byte. Doing `y = 7` in the initialization in the `for` loop is insufficient. You can remove it and do an explicit `y = 7;` just above your `while`. – Craig Estey Jul 06 '23 at 17:31

1 Answers1

0
for (int i = 0; i < strlen(message); i++)
{
    int k = message[i];
    int counter = 0;
    int bits[BITS_IN_BYTE];
    while (counter < BITS_IN_BYTE)
    {
        if (k % 2 != 0)
        {
            bits[counter] = 1;
        }
        else
        {
            bits[counter] = 0;
        }
        k = k / 2;
        counter++;
    }
    
    // here we are giving the binary values from right to left 
    for (int reverse = 8; reverse >= 0; reverse--)
    {
        print_bulb(bits[reverse]);
    }

    printf("\n");
}

}

yamilobt8
  • 1
  • 1
  • if this help you press the upper button – yamilobt8 Jul 14 '23 at 14:23
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 17 '23 at 20:25