#Decimal to Binary

1 messages · Page 1 of 1 (latest)

ornate copper
#

This code is supposed to convert a decimal to a 16-bit binary. Can you guys please help me understand lines 10-11 and 22-23, thanks

#include <stdio.h>

int main() {
    // Declare variables
    unsigned int decimal, binary = 0;
    int i;

    // Ask for user's input
    printf("Enter non-negative decimal integer to convert: ");
    if (scanf("%u", &decimal) != 1) {
        printf("Error occured\n");
        return 1; // Exit with an error code
    }

    // Check if the input number can be represented in 16 bits
    if (decimal > 65535) {
        printf("Error occured\n");
        return 1; // Exit with an error code
    }

    // Convert decimal to binary
    for (i = 15; i >= 0; i--) {
        int bit = (decimal >> i) & 1;
        printf("%d", bit);
    }

    printf("\n");

    return 0; // Exit without an error code
}
slate tartanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

fair kayak
#

What are the lines 10-11 and 22-23?

ornate copper
#

if (scanf("%u", &decimal) != 1) and c for (i = 15; i >= 0; i--) { int bit = (decimal >> i) & 1; basically

fair kayak
#

scanf returns how many conversions it successfully did (should be 1) and %u is the format specifier for an unsigned integer.

#

The loop just checks bit by bit (from most signficiant to least significant) whether the current bit is set

ornate copper
dull ice
#

how many % formatters it matched

fair kayak
#

Take the input This is a sentence.
Now let's test it out:

#

;compile | This is a sentence longer than five words

#include <stdio.h>

char a[10] = { 0 }, b[10] = { 0 }, c[10] = { 0 }, d[10] = { 0 }, e[10] = { 0 };
int conversions = scanf("%s %s %s %s %s", a, b, c, d, e);
printf("Got %d conversions for input %s %s %s %s %s\n", conversions, a, b, c, d, e);
neat nebulaBOT
#
Program Output
Got 5 conversions for input This is a sentence longer
ornate copper
#

So basically, in our code, scanf must perform EXACTLY ONE conversion of the user's input

#

For the rest of the program to work

fair kayak
#

Yeah, but because it accepts at most 1 conversion it needs to perform at least one

ornate copper
#

Would the program NOT work without ```c
if (scanf("%u", &decimal) != 1) {
printf("Error occured\n");
return 1; // Exit with an error code
}

fair kayak
#

it would run into problems

dull ice
#

it's useful here, since %u only matches unsigned integers, so if the user types in e.g. "abc" it won't match , and it'll return 0

fair kayak
#

;compile | a

#include <stdio.h>

unsigned x;
int conversions = scanf("%u", &x);
printf("got %u with %d conversions\n", x, conversions);
neat nebulaBOT
#
Program Output
got 0 with 0 conversions
fair kayak
#

So yeah, it catches the case where you enter e.g. just an a

#

or a negative number

#

or anything other that is not an unsigned integer

ornate copper
#

if (scanf("%u", &decimal) != 1) { // Checks if user's input is an unsigned integer is this comment appropriate here?

dull ice
#

I generally prefer commenting the conditional block as a whole, rather than the if itself, so I'd probably word it closer to "if the input successfully matched"

#

but the best comments are ones that make the most sense to you, since they're notes for yourself

fair kayak
#

I think that comment is true, but tbh I try to avoid scanf whenever I can. That thing's a nasty bitch

ornate copper
#

Got y'all

#

All in all, does this code have any flaws?

#

Or does it successfully convert decimal to binary

dull ice
#

try it and find out

ornate copper
#

It seems to work perfectly fine for me

#

No unintended errors

#

Conversions are accurate

dull ice
#

any warnings?

fair kayak
#

Are you even compiling with warnings enabled?
-Wall -Wextra -Wpedantic?

ornate copper
#

Just compiling through Terminal

fair kayak
#

hmmm

ornate copper
#

That's intended though

fair kayak
#

Ah, not bigger than 16 bits

ornate copper
#

Can't be represented as a 16-bit number

fair kayak
#

I see

ornate copper
#

Right

fair kayak
#

;compile -Wall -Wextra -Wpedantic | 8191

#include <stdio.h>

int main() {
    // Declare variables
    unsigned int decimal, binary = 0;
    int i;

    // Ask for user's input
    printf("Enter non-negative decimal integer to convert: ");
    if (scanf("%u", &decimal) != 1) {
        printf("Error occured\n");
        return 1; // Exit with an error code
    }

    // Check if the input number can be represented in 16 bits
    if (decimal > 65535) {
        printf("Error occured\n");
        return 1; // Exit with an error code
    }

    // Convert decimal to binary
    for (i = 15; i >= 0; i--) {
        int bit = (decimal >> i) & 1;
        printf("%d", bit);
    }

    printf("\n");

    return 0; // Exit without an error code
}
neat nebulaBOT
#
Program Output
Enter non-negative decimal integer to convert: 0001111111111111
Compiler Output
<source>: In function 'main':
<source>:5:27: warning: unused variable 'binary' [-Wunused-variable]
    5 |     unsigned int decimal, binary = 0;
      |                           ^~~~~~
fair kayak
#

@ornate copper There is one warning for you

ornate copper
#

Build failed?

fair kayak
#

Because of -Werror all warnings are being treated as errors, so it fails to build even if there's just a warning

#

Remove that and as you can see it will still work, but will also throw you the warning

fair kayak
#

Yes

ornate copper
#

Completely unrelated, but can you please help me understand another program

#
int main() {

    char UserInput;
    int ShiftAmount;

    printf("Enter lower-case letter to encrypt: ");
    scanf("%c", &UserInput);

    if (UserInput < 'a' || UserInput > 'z') {
        printf("Error: user did not enter lower-case letter, exiting\n");
        return 1;
    }

    printf("Enter the shift amount for Caesar cipher: ");
    scanf("%d", &ShiftAmount);

    // Ensure the shift amount is within (1, 25)
    ShiftAmount = ShiftAmount % 26;
    if (ShiftAmount < 0) {
        ShiftAmount = ShiftAmount + 26;
    }

    // Perform the Caesar shift
    char Shifted = 'a' + (UserInput - 'a' + ShiftAmount) % 26;

    printf("Ciphertext is %c\n", Shifted);

    return 0;
}```
#

Specifically this part char Shifted = 'a' + (UserInput - 'a' + ShiftAmount) % 26;

dull ice
#

what about it

ornate copper
#

I don't understand what the line does

dull ice
#

based on the variable name, it's calculating what the shifted character corresponding to your input is

ornate copper
#

Right, I get that

#

I just don't understand the code itself

dull ice
#

what about it?

ornate copper
#

'a' + (UserInput - 'a' + ShiftAmount) % 26

dull ice
#

what part of that

ornate copper
#

Why are there 'a's

#

And why does mod 26 intervene here

dull ice
#

'b' - 'a' = 1 (with ASCII / the encoding scheme you posted)

dull ice
slate tartanBOT
#

@ornate copper Has your question been resolved? If so, run !solved :)

ornate copper
#

!solved