Please explain this code | SoloLearn: Learn to code for FREE!

+27

Please explain this code

Why is the output 42? Is it because 36+6=42? https://code.sololearn.com/cuywIHx85X4k/?ref=app

1/24/2021 7:18:22 AM

ChillPill

23 Answers

New Answer

+17

Hi ChillPill, I think its because number of bits for <a> and <b> are limited. Code Playground gave warning when we run this code, explaining what happened. DEC BIN 36 0010 0100 4 0 0100 ( only 5 bits allowed ) 6 0000 0110 2 10 ( only 2 bits allowed )

+17

thanks guys.

+13

No, it's writing 4 2 You are assigning 36 (100100) to a 5 bit field, so it takes only the first 5 bits: a=00100=4 And you are assigning 6(110) to a 2 bit field: b=10=2

+7

JaScript yes, an 8 bit value that represents the 7 segments display could be great use of bitfields.

+5

1. 36 (<unsigned char:8>) is converted into <unsigned char:5>. 36 mod (2 ^ 5) = 36 mod 32 = 4. Unary plus converts it into <int>. Output1 = "4"; 2. 6 (<unsigned char:8>) is converted into <unsigned char:2>. 6 mod (2 ^ 2) = 6 mod 4 = 2. Unary plus also converts it into <int>. Output2 = "2"; FinalOutput = Output1 + Output2 = "42" (Late with answer because of infinite posting answer glitch)

+5

Please don't spam here

+4

Abdalrhman Wa post a separate question for that.

+3

When you remove << string(42,'\n') from this code, you will get two warnigs with explanation, what happend. I could imagine a control for digital display similar to 7-segment display.

+3

JaScript, string(42, '\n') just creates the string with 42 newlines. There are still warnings, but you need to scroll down (on SoloLearn Playground) to see them.

+2

get thanks for the explanation.

+2

Abdalrhman Wa, hmm... We do not solve exam assignments for others, we can only fix errors and give clues on how to make it.

+2

0

Abdalrhman Wa, even if I want to help you, I cannot: learning C# is currently on my plans only 😂. And it is not necessary to have a computer or laptop to code (but most of people prefer to program at PC/laptop — better tools, larger screen, etc.). You can use this app — SoloLearn; C# online compiler — https://play.google.com/store/apps/details?id=com.krazeapps.csharpprogrammingcompiler, https://apps.apple.com/id1451066693; for Android — offline C# shell: https://play.google.com/store/apps/details?id=com.radinc.csharpshell; for iOS — (hope that good) program for C# and F#: https://apps.apple.com/id1095213378 (paid); https://apps.apple.com/id1452106609. You can also watch others' codes here (Code Payground -> "Language: C#" filter). And, like @Sonic said, post a new question and do not try to get answer on a solved question — thus, there will be greater chance for your question to be solved (if I would have unfollowed to this question, there would be 60% chance of you being unnoticed).

0

Abdalrhman Wa, I did not learn C# yet, so how am I supposed to write code???

0

Now two code-beggers... I get out of here...

0

https://www.sololearn.com/post/915539/?ref=app

-3

I have exam tomorrow at 8 PM, in Composition & aggregation & static

-3

Write a function namely multiply of type int that takes two arguments of type int then call the function in main function. The function will return the multiplication of the two arguments.

-4

-4

get please just simple code. I don't have pc or laptop