Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 23rd, 2007, 6:26 PM   #1
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Interesting Bitwise tricks

I just stumbled upon this page: Bitwise Tricks and thought it was really interesting. What do you guys think?
__________________

Wizard1988 is offline   Reply With Quote
Old Nov 23rd, 2007, 6:58 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: Interesting Bitwise tricks

To be used when needed, not to make things fancy but hard to read.
A few, such as number & 1 to test even/odd, I deem straightforward enough for every day use. Others may break at some point in the future, in an entirely unforeseen and puzzling way. Which may not be worth it to save a cycle in a function that gets called twice a week.
So, as with just about everything, it's a tradeoff. Picking up cool tricks is easy. The hard part is knowing where they fit.

And then, there's stuff like this...
http://www.beyond3d.com/content/articles/8/
That's the kind of optimization worthy of praise.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Nov 23rd, 2007, 9:30 PM   #3
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 105
Rep Power: 0 null_ptr0 is an unknown quantity at this point
Re: Interesting Bitwise tricks

grats on figuring out an age old set of usages of the standard binary/bitset operations that replace the use of memory consuming 'booleans' in java.
null_ptr0 is offline   Reply With Quote
Old Nov 23rd, 2007, 11:28 PM   #4
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: Interesting Bitwise tricks

It's interesting that, although the subject was touched upon in school, we were told it's usually not a good idea to use bitwise operations. Guess that's what I get for getting an associates at ITT?
Jabo is offline   Reply With Quote
Old Nov 23rd, 2007, 11:41 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Interesting Bitwise tricks

If your compiler optimizes a mod-two operation into a bitwise operation, then you're good to go. If it doesn't, it's a waste. Many people don't even realize there's an alternative. A highly-skilled programmer realizes there is an alternative, and a highly-skilled programmer is not flabbergasted and helpless if he or she sees if (!(a & 1)).

Admittedly, I'm no longer an embedded programmer working with tiny amounts of resources. Consequently, I no longer look to see if a multiplication or division involves a power of two. In the first place, that number might change, someday, to a non-power of two, and my code would break.

I would also offer the opinion that if one is resorting to these kinds of tricks, from necessity, then one might wish to drop into assembly language and avoid the extraneous stuff that a compiler routinely emits.

On the other side of the coin, a programmer can be a good, productive programmer without knowing what goes on under the hood.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 24th, 2007, 12:24 AM   #6
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Re: Interesting Bitwise tricks

I also hear that allot of tricks like this pop up on interview tests. The way some of these tricks work is common sense, but like Dameon said figuring out where they fit is the hard part.

Easy logical operator trick

int* a = NULL;

*a = 8; //seg fault

if(a && (*a = 10)); // false, no seg fault

If you know any other interesting tricks please share

Also if anyone is really interested in things like this I suggest reading "Computer Systems - A Programmers Perspetive" by Randal Bryant and David O Hallaron
__________________

Wizard1988 is offline   Reply With Quote
Old Nov 24th, 2007, 12:38 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Interesting Bitwise tricks

There's no seg fault there because there's no dereference. The fact that a is NULL (false) shortcircuits the evaluation so that (*a = 10) is neither assigned nor evaluated.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 25th, 2007, 1:00 AM   #8
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: Interesting Bitwise tricks

@Wizard

I wouldn't call that a...trick. Especially not a 'bitwise' trick. Logical AND (&&, not &) does not need to evaluate the right side because there's no way that an AND could be true if the left is false. Notice the 'true' and 'false' -- the logical operators work with booleans. Bitwise AND on two integers (such as 0 & 15) behaves in a somewhat similar way but in an entirely different fashion. If you consider 0 as false, then 0 & 15 will yield false as expected, but only because AND was performed on the two sets of bits. Your example might be filed under basic understanding of the language.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Nov 25th, 2007, 1:10 AM   #9
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Re: Interesting Bitwise tricks

Yes, I understand that that wasn't a bitwise trick. What I was trying to demonstrate is that there are things which are common sense but applying them in the right place is what is hard as you have said.
__________________

Wizard1988 is offline   Reply With Quote
Old Nov 25th, 2007, 8:09 PM   #10
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Re: Interesting Bitwise tricks

Quote:
Originally Posted by Wizard1988 View Post
int* a = NULL;

*a = 8; //seg fault

if(a && (*a = 10)); // false, no seg fault
You don't need the 'if': a && *a=10;
However, that usually boils down to: 'if (a) *a = 10;' anyway
Harakim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Some of you may find this interesting. Fall Back Son Coder's Corner Lounge 2 May 19th, 2007 7:47 PM
bitwise operators and type double ionexchange C++ 14 Feb 12th, 2006 10:27 AM
need help with bitwise operations metsfan C 17 Feb 2nd, 2006 7:09 PM
Interesting new ideas sixstringartist Coder's Corner Lounge 21 Jun 27th, 2005 9:38 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC