+ 1

Boolean Rules explanation please!

Give me an example or Clear definition to: "If a Boolean value is assigned to an integer, true becomes 1 and false becomes 0. If an integer value is assigned to a Boolean, 0 becomes false and any value that has a non-zero value becomes true."

6th Nov 2016, 4:00 AM
Billy Anak Leo
Billy Anak Leo - avatar
3 ответов
+ 2
boolean values are two- true(represented by 1) and false (represented by 0). for the computer every string and number is represented in binary so the number 0 Is 0000 , 1 is 0001, 2 is 0010, 3 is 0011 and so on. an empty string is again represented as 0000. the boolean value false is represented as 0000. So when you cast (convert a number or a string) to boolean, you either get 0000 (if the number is 0 or you have an empty string) or you get another binary number. If this binary number is 0000 then you get false, if the number is different from that(it doesn't matter what) you get true. Now if you have the number 11 witch in binary is 1011 or 12 witch is 1100, the computer will turn it into 0001 because it doesn't need the other digits to determine is it true or false and if it saved them it will be a loss of memory. And that is why if you try to convert it again to integer you will get 1
6th Nov 2016, 2:04 PM
Jax
Jax - avatar
0
*this is not an exact explanation of how data is represented but I think it's close to reality and good enough for beginners.
6th Nov 2016, 2:07 PM
Jax
Jax - avatar
0
I understand . that is the fundamental of number arrangements in every base .. so basically Boolean have only two digit in base boolen .. 0 and 1
13th Dec 2016, 7:51 AM
Billy Anak Leo
Billy Anak Leo - avatar