Coder Profile - Show off your skills, get a coder profile.
 
 
 
Example Framework Implementation 284 2
PHP (Hypertext Preprocessor) Views Comments
MS SQL Pagination 364 0
SQL (Structured Query Language) Views Comments
MS SQL - MySQL for indra 414 0
SQL (Structured Query Language) Views Comments
Bitwise Boolean Inverse 417 4
C Views Comments
PHP-GTK Port Scaner 403 0
PHP (Hypertext Preprocessor) Views Comments
Page 1 of 1
Bitwise Boolean Inverse
  
Public URL http://www.coderprofile.com/pinned-code/207/bitwise-boolean-inverse
Language C
Expires Never
Length 36 Characters (3 Lines)
Password no password
Download Download Source Code
Source Code
Inverse a boolean value using bitwise &
  1. bool b = false;
  2.  
  3. b = ((b + 1) & 1)
Please login to post comments.
 
Craige     Posted 1.62 Year Ago
 
 
I don't know, lol. Didn't really care when I wrote it. Thanks for the tip
though!
 
Uranium-239     Posted 1.62 Year Ago
 
 
In that case, why didn't you just do:

b = ~b & 1

?

The complement operation is faster than an addition.
 
Craige     Posted 1.62 Year 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 1.62 Year 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
Craige Leeder (20)
Canada, Ontario
Craige has 5 fans
become a fan
» Applications
Source Codes Source Codes (2)
Code Pin Board Code Pin Board (5)
» About Me
About Me About Me
Friends Friends (17)
Résumé Résumé
» Misc
Overview Overview
Send A Friend Invite
Send Message Send A Message
RSS Whole Profile Feed
 
 
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
1.77 Year Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2010, Scott Thompson, All Rights Reserved