Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I came across a code that looks something like:
bool[] leds = new bool[3*64];
int[] audionr = new int[4];
int audiosound;

public int AudioSound
{
get {return audiosound;}
set {audiosound = value;}
}

....//further on in code
leds[audionr[0]] = !Convert.ToBoolean(this.AudioSound & 0x001);


What does this "!Convert.ToBoolean" do/mean in this scenario?
Posted
Updated 15-Oct-15 1:23am
v2

! is the binary negation operator.
!true returns false
!false returns true

Convert.ToBoolean(this.AudioSound) producing a boolean value from an int value (meaning, returns false if int value is zero, otherwise returns true), this code is assigning the boolean value at index zero in leds table to the negation of the conversion of the int value AudioSound.

As to why this code doas that, it's impossible to answer without studying the algorithm and figure out how the leds table is used.
 
Share this answer
 
Comments
Krunal Rohit 15-Oct-15 7:21am    
Ta, 5!

-KR
Member 11971544 15-Oct-15 7:31am    
Hi @phil.o, thanks for your response just updated the code as I missed a little part out. I'm trying to fully comprehend your explanation, are you saying:
!Convert.ToBoolean(this.AudioSound & 0x001)means if AudioSound = 0, it will return false and if its > 0 then it returns true?
phil.o 15-Oct-15 7:38am    
(this.AudioSound & 1) just transform the original AudioSound int value into zero or one, depending on the fact it is odd or even (one for odd, zero for even).
If (this.AudioSound & 1 == 0) (AudioSound is even), Convert.ToBoolean(this.AudioSound & 1) will return false, and the negation will make the result true.
If (this.AudioSound & 1 == 1) (AudioSound is odd), Convert.ToBoolean(this.AudioSound & 1) will return true, and the negation will make the result false.
Member 11971544 15-Oct-15 7:40am    
I get it now. Thank you.
Member 11971544 15-Oct-15 9:03am    
what if it looks something like: !Convert.ToBoolean(this.AudioSound & 0x002)what is the transformation here?
!Convert.ToBoolean() mean that it just negate the result coming from Convert.ToBoolean()
if Convert.ToBoolean() return true then !Convert.ToBoolean() is false and if Convert.ToBoolean() return false then !Convert.ToBoolean() is true.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900