Jump to content

if command

Liam mallka

image.png.8978864b7a64332855ed92612efcb4fd.pngI thought that the answer here is always because -8 and 7 arent 0 but its actually false, any ideas why?

 

Link to comment
Share on other sites

Link to post
Share on other sites

& is a bitwise AND operator. Opening the calculator reveals that -8 is represented as

1111 1111 1111 1000

in a 16bit integer

 

image.png.d57720c68c96cf5b6ec363d2ced9b1cf.png

whereas 7 is

0000 0000 0000 0111

image.png.7c582b78f39cad529601d7775c38dabb.png

 

and guess what,

 

1111111111111000 & 0000000000000111 == 0000000000000000 == FALSE

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

The above is how & works, you likely intended to use && instead.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

the & operation first compares the 2 numbers and outputs true or false. since -8 and 7 are not the same, "-8 & 7" returns false. so it goes into the else statement.

image.png.7769045608dc3e0e43d5a26a95a91aa6.png

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, The_Vaccine said:

the & operation first compares the 2 numbers and outputs true or false. since -8 and 7 are not the same, "-8 & 7" returns false. so it goes into the else statement.

Why do you feel you need to post a completely wrong explanation when @Saurongave an extremely detailed and correct one?

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, Kilrah said:

Why do you feel you need to post a completely wrong explanation when @Saurongave an extremely detailed and correct one?

what is wrong about what I said?

Link to comment
Share on other sites

Link to post
Share on other sites

  

8 minutes ago, The_Vaccine said:

what is wrong about what I said?

 

35 minutes ago, The_Vaccine said:

since -8 and 7 are not the same, "-8 & 7" returns false

 

This. If any of the bits were 1 in both numbers the operation would return true even if the numbers are different. Read Sauron's post.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, The_Vaccine said:

what is wrong about what I said?

You said "the & operation first compares the 2 numbers" which is incorrect. & is a binary operator. It does not compare anything. It performs a binary "and" operation, which results in a "new" number. This number is then evaluated by the if statement. Any number != 0 is true.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×