Hey guys I am trying to do HW 5 for the beginner series but having some issues, does anyone know why my reticle isn't doing the colour change properly?
also how come you need to write 2 "&" instead of 1 "&" during the bool statements? I tried running the homework download source code with just 1 & instead of 2 && and it still ran?
Thanks for the help.
HW 5 (Beginner) Confusion
- Circumnavigate
- Posts: 5
- Joined: October 27th, 2019, 12:04 am
- Location: Irvine, California
HW 5 (Beginner) Confusion
- Attachments
-
- HW 5.zip
- (84.79 KiB) Downloaded 136 times
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: HW 5 (Beginner) Confusion
Chili will or should have covered this, but one & is for bitwise operations between integral types:
0b11001010 &
0b10000000 =
---------------------
0b10000000
The double && ( double ampersand ) is logical AND
true && true = true
true && false = false
false && true = false
false && false = false
The reason it works is because a bool is convertible to an int. When converted, false equals 0 and true converts to 1.
0b00000001 & 0b00000001 = 0b00000001 = 1
0b00000000 & 0b00000001 = 0b00000000 = 0
0b00000001 & 0b00000000 = 0b00000000 = 0
0b00000000 & 0b00000000 = 0b00000000 = 0
Then, for the if statement, the int is converted back to bool where a 0 is false and any non-zero value is true.
If you were comparing two floats for instance, the single ampersand which requires integral types would fail to compile.
0b11001010 &
0b10000000 =
---------------------
0b10000000
The double && ( double ampersand ) is logical AND
true && true = true
true && false = false
false && true = false
false && false = false
The reason it works is because a bool is convertible to an int. When converted, false equals 0 and true converts to 1.
0b00000001 & 0b00000001 = 0b00000001 = 1
0b00000000 & 0b00000001 = 0b00000000 = 0
0b00000001 & 0b00000000 = 0b00000000 = 0
0b00000000 & 0b00000000 = 0b00000000 = 0
Then, for the if statement, the int is converted back to bool where a 0 is false and any non-zero value is true.
If you were comparing two floats for instance, the single ampersand which requires integral types would fail to compile.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: HW 5 (Beginner) Confusion
Two of the signs are wrong in this if statement
Code: Select all
if (left_mobile < right_fixed &&
right_mobile < left_fixed &&
top_mobile < bottom_fixed &&
bottom_mobile < top_fixed)
{
colliding = true;
}
else
{
colliding = false;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com