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
#define CPP ChillPill
23 Answers
New AnswerHi 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 )
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
JaScript yes, an 8 bit value that represents the 7 segments display could be great use of bitfields.
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)
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.
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.
Abdalrhman Wa, hmm... We do not solve exam assignments for others, we can only fix errors and give clues on how to make it.
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).
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.