Coder Profile - Show off your skills, get a coder profile.
 
 
 
code pin board Bitwise Boolean Inverse Download Source Code
Author Details Code Information
Craige ( Craige Leeder )

Pinned 5 Codes
Posted 0 Coding Articles

Send A Message
View Coders Profile
Language C
Expires Never
Length 36 Characters (3 Lines)
Password no password
Description

Inverse a boolean value using bitwise &
  1. bool b = false;
  2.  
  3. b = ((b + 1) & 1)
code pin board Back To Code Pin Board Post New Code
Please login to post comments.
 
Craige     Posted 3.4 Years Ago
 
 
I don't know, lol. Didn't really care when I wrote it. Thanks for the tip
though!
 
Uranium-239     Posted 3.5 Years Ago
 
 
In that case, why didn't you just do:

b = ~b & 1

?

The complement operation is faster than an addition.
 
Craige     Posted 3.5 Years Ago
 
 
Incorrect. False is 0, and True is 1. The nature of a boolean is that it only has 2
possible values.

What you are getting into is the evaluation of a value as a boolean. In almost all
programming languages, you are correct. 0 evaluates to False, and anything but will
evaluate to True. However, this does not make True equal to anything but False.

My code is perfectly acceptable (though unreasonable to use an production code).
 
Uranium-239     Posted 3.5 Years Ago
 
 
This is incorrect and should not be used in applications as it is exploitable.

The correct code would be:

bool bvar = false;
bvar = !bvar; // bvar is now true
bvar = !bvar; // bvar is now false etc...

The reason being is that with boolean values, false is 0 and true is ANYTHING else
(not always 1).
Page 1 of 1
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.20 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved