39
u/OriAfias Feb 02 '22
so 2 is false?
7
Feb 03 '22
This was meant to convert registry keys to bools, like if a reg key was true or int32 one, this would return true.
2
u/MeatyLabia Feb 03 '22
In C# when you use Convert.ToBoolean 1 is true and every other value is false.
→ More replies (2)3
140
u/catithebathtub Feb 02 '22
th- this isn't just bad, it's wrong
32
Feb 02 '22
Funny thing is: it works.
96
u/Willinton06 Feb 02 '22
Cause not only is it wrong, but you’re also using it wrong, so it evens out
6
u/FrezoreR Feb 03 '22
So, two wrongs makes a right after all?
13
44
u/Knuffya Feb 02 '22
it is wrong though.
int(34) should be true. just as any integer > 0
5
→ More replies (9)1
u/0Flight64 Feb 03 '22 edited Feb 03 '22
Not exactly. The language looks like Java to me. At least in Java 8, 1 is not equivalent to true and 0 is not false. Even in the tutorials I see geeksforgeeks, they are using an if - else statement for the conversion. Edit: Just realised this is C#. Anyway, same issue here so. Implicit conversion from int to bool is not allowed.
4
u/MrBananaStorm depraved Feb 03 '22
It's C#, and apparently they are using the unity engine which stores bools as 0 or 1 integers, which is why this works. They are only converting that 0 or 1 to false or true, in theory it should never be a 2 or 3, if it is, something is messing up elsewhere.
Doesn't excuse the fact it's still badcode tho lol.
→ More replies (4)42
8
Feb 03 '22
Not really, booleans are typically stored as a byte, and it's value can range from 0 - 255. 0 is false, and everything else is normally true. In this instance, everything but 1 is false.
3
u/RFC793 Feb 03 '22
What environment normally stores integers as a byte? Yes, it is never a “bit” as that isn’t addressable. More typically the machine’s word size, 32 bit, or the min(32, word size)…
→ More replies (2)10
Feb 03 '22
Let's all be thankful that bit-endianess is generally only a headache of hardware developers.
→ More replies (1)4
132
u/joske79 Feb 02 '22
You should add cases for all possible int values.
18
u/Eluvatar_the_second Feb 03 '22
Enter source generators
1
u/aaaantoine Feb 03 '22 edited Feb 03 '22
foreach (int i in Enumerable.Range(int.MinValue, int.MaxValue - int.MinValue)) Console.WriteLine($"case {i}: return {i == 1};");Edit: nope. Trying again.
int i = int.MinValue; do { Console.WriteLine($"case {i}: return {(i == 1).ToString().ToLower()};"); i++; } while (i != int.MinValue);2
u/ExceedingChunk Feb 19 '22
You can optimize it by creating a hashmap of all int values as keys and all the boolean outputs as value!
56
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 02 '22
Image Transcription: Code
public static bool ToBool(int input)
{
//woooo switch case
switch (input)
{
case 0:
return false;
case 1:
return true;
default:
return false;
}
}I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
13
10
8
u/tony7797176 Feb 02 '22
I guess its not a big deal; “ if (1) return true; return false; “ maybe just be a little simpler.
46
u/hareppas Feb 02 '22
or just
return input == 1;→ More replies (1)11
u/tony7797176 Feb 02 '22
Good point lol. I just wrote bad code lol
9
u/SoulArthurZ Feb 02 '22
you didn't write bad code, it could just be simplified to return input == 1
6
u/ArfieCat Feb 03 '22
awesome, this is just what i needed!
public static bool IsEven(int input)
{
if (!ToBool(input % 2))
{
return false;
}
return true;
}4
u/Alhoshka Feb 03 '22 edited Feb 03 '22
"woooo switch case"
I take it you were just beginning your learning journey?
That's not bad code, that's adorable.
.
it's still bad code though, don't do that
3
3
3
1
Feb 03 '22
Did you try:
if(input == 0) {
return false;
} else if(input == 1) {
return true;
} else {
return false;
}^ that seems better efficient :P
6
1
1
1
Feb 03 '22
I love how Visual Studio even highlighted that the switch case is absolutely unnecessary lol
1
u/MeatyLabia Feb 03 '22
It is suggesting to convert to a switch expression. Not unnecessary at all.
1
Feb 04 '22
I think you misread my comment. I meant that the switch case is unnecessary, not the highlighting.
1
u/MeatyLabia Feb 04 '22
You said vs highlighted that the switch case is unnecessary, which it didnt highlight it for.
1
1
1
1
u/Ambitious_Medium_625 Nov 09 '22
Why do an if statement when you can use a switch statement and overcomplicate your code? That's a big play right there.
0
u/zsharp68 Feb 03 '22
return (input == 1) ? true : false;
6
4
1
254
u/Ok-Mulberry-4600 Feb 02 '22
Seems like someone was keen to use a switch statement