static void Main(string[] args)
{
bool a, b;
unsafe
{
int* pa = (int*)&a;
int* pb = (int*)&b;
*pa = 1;
*pb = 2;
}
Console.Write(Test(a, b));
}
This prints True
for me with the C# implementation that comes with Visual Studio 2015. I actually don't know any C#, but I figured I'd try to write some C code and see if it worked. I was hoping that the compiler would assume that True is always represented as 1 and use a bitwise AND. In Debug mode, this is indeed the case (it worked with Release too). It uses a bitwise AND for the first condition and two comparisons to zero for the second:
if (a && b) return false;
002C2E92 movzx eax,byte ptr [ebp-3Ch]
002C2E96 movzx edx,byte ptr [ebp-40h]
002C2E9A and eax,edx
002C2E9C and eax,0FFh
002C2EA1 mov dword ptr [ebp-44h],eax
002C2EA4 cmp dword ptr [ebp-44h],0
002C2EA8 je 002C2EB2
002C2EAA xor edx,edx
002C2EAC mov dword ptr [ebp-48h],edx
002C2EAF nop
002C2EB0 jmp 002C2EE4
if (a) if (b) return true;
002C2EB2 movzx eax,byte ptr [ebp-3Ch]
002C2EB6 mov dword ptr [ebp-4Ch],eax
002C2EB9 cmp dword ptr [ebp-4Ch],0
002C2EBD je 002C2EDC
002C2EBF movzx eax,byte ptr [ebp-40h]
002C2EC3 mov dword ptr [ebp-50h],eax
002C2EC6 cmp dword ptr [ebp-50h],0
002C2ECA je 002C2EDC
002C2ECC mov eax,1
002C2ED1 and eax,0FFh
002C2ED6 mov dword ptr [ebp-48h],eax
002C2ED9 nop
002C2EDA jmp 002C2EE4
return false;
002C2EDC xor edx,edx
002C2EDE mov dword ptr [ebp-48h],edx
002C2EE1 nop
002C2EE2 jmp 002C2EE4
}
002C2EE4 mov eax,dword ptr [ebp-48h]
002C2EE7 lea esp,[ebp-0Ch]
002C2EEA pop ebx
002C2EEB pop esi
002C2EEC pop edi
002C2EED pop ebp
002C2EEE ret
4
Welcome to PPCG! All challenges here need an objective winning criterion such that a winner can be chosen if there are multiple submissions. It seems there might only be a single solution here, so this challenge might not be a good fit for PPCG. For future challenges let me recommend the sandbox where you can get feedback before the challenge goes live.
– Martin Ender – 2016-02-28T10:49:01.017(That said, I am intrigued by this puzzle, it's just that normally there should be multiple possible solutions to a challenge for it to make sense around here.) – Martin Ender – 2016-02-28T10:57:37.440
@MartinBüttner My bad sorry. But why can't there be only one solution? Its a puzzle and not a code-golf challenge. – a-ctor – 2016-02-28T11:01:28.403
2
Meta discussion is split on whether programming puzzles without additional win criteria are on topic, with conflicting answers being upvoted. I'd rather keep questions open when it's unsettled, so I'm voting to reopen. If you have opinions, please contribute them to the discussion.
– xnor – 2016-02-28T11:10:40.850Ah well, programming puzzles have a bit of a weird place in this community, despite being in the name (and it's been suggested several times to remove it there). The main problem is that only very few puzzles fit the spirit of the site. In any case, whether there is only one solution or not, this needs an objective winning criterion in case someone does find a second one. – Martin Ender – 2016-02-28T11:11:02.267
1@xnor Thanks for the reminder. I guess I'll reopen and let the community decide then. – Martin Ender – 2016-02-28T11:14:23.110
@MartinBüttner Added a winning condition to my puzzle. – a-ctor – 2016-02-28T11:21:08.003
1@MartinBüttner I don't know about this one. One can just execute this code 4 times and "brute force" the solution. I am generall open for this kind of challenge, but this one is way too trivial imo. – Denker – 2016-02-28T11:26:37.223
2@DenkerAffe I don't think any of the four obvious parameter combinations work. – Martin Ender – 2016-02-28T11:27:09.643
1@DenkerAffe You can try the 4 common bool combinations but you will only end up with
false
– a-ctor – 2016-02-28T11:30:17.7673assuming there is a valid answer, this is an excellent question, regardless of whether it fits what we normally consider on topic.+1. I think one of the reasons we don't normally consider questions like this on topic is that every other question I've seen like this has been by a beginner, and the answer has been blindingly obvious. – Level River St – 2016-02-28T11:50:35.780
@MartinBüttner Oh, my bad. I agree then, lets just see how the community responds to it. – Denker – 2016-02-28T11:52:27.813
Holy WOW this is a nice paradox, no idea how to solve it. :O – timmyRS – 2016-02-28T12:04:06.617
Is there any restriction on how much use of reflection is allowed? – Martin Ender – 2016-02-28T12:18:13.110
@MartinBüttner You can use it of course - no restriction. Just don't change the method ;) – a-ctor – 2016-02-28T12:23:03.210
1@MartinBüttner Just a note tho: My solution does not use reflection. – a-ctor – 2016-02-28T13:16:35.343
5@Widi See that's why "first valid solution" might not be the best idea. You might get an uninteresting but working solution which just messes with some internals via reflection and then there's no incentive for anyone to go looking for a more interesting solution without reflection. – Martin Ender – 2016-02-28T13:30:15.407
Are we strictly limited to C#, or can we implement this identically in another language and show a solution for that language? – Addison Crump – 2016-02-28T13:31:39.873
@VoteToClose I think its a C# only problem. – a-ctor – 2016-02-28T13:54:25.217