Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gem about bit manipulations #266

Merged
merged 1 commit into from
Aug 10, 2016

Conversation

wilzbach
Copy link
Member

Also based on #224 (will be rebased) - fixes #130.

@wilzbach wilzbach changed the title [WIP] Bit manipulations [WIP] Add gem about bit manipulations Jun 14, 2016
@wilzbach wilzbach force-pushed the bit_manipulations branch 3 times, most recently from 2235ce9 to a08501b Compare June 14, 2016 16:54
@JackStouffer
Copy link
Contributor

I think the gems section needs a navigation redesign. I agree that it's a cool idea to present these ideas in the tour format. But if you're going to have lots of gems as your PRs imply, they shouldn't be accessed from a dropdown. Preferably, the nav would link to a page with the gems grouped into beginner and advanced techniques.

@wilzbach
Copy link
Member Author

I think the gems section needs a navigation redesign. I agree that it's a cool idea to present these ideas in the tour format. But if you're going to have lots of gems as your PRs imply, they shouldn't be accessed from a dropdown. Preferably, the nav would link to a page with the gems grouped into beginner and advanced techniques.

I fully agree - see #269 (restructure chapters) and #52 (vertical left menu).

@wilzbach wilzbach force-pushed the bit_manipulations branch 2 times, most recently from af8a5c3 to 58f5c14 Compare June 20, 2016 22:09
@wilzbach wilzbach changed the title [WIP] Add gem about bit manipulations Add gem about bit manipulations Jun 20, 2016
@wilzbach
Copy link
Member Author

Ready for destruction ;-)

Out of interest does anyone know whether the fact that bitfields are no lvalues is a feature or bug?

vec.x = 2; // OK
vec.x += 2; // ERROR: vec.x is no lvalue

@stonemaster
Copy link
Collaborator

@wilzbach I would consider this "as designed". Bitfields need to do bit fiddling internally and if lvalue ought to be supported an opaque type would have to be returned that writes changes back. At first this sounds doable but increased the complexity and maybe hurts performance a little..


### Simple bit manipulation

D offers the following operator for bit manipulation:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operators

@wilzbach
Copy link
Member Author

@wilzbach I would consider this "as designed". Bitfields need to do bit fiddling internally and if lvalue ought to be supported an opaque type would have to be returned that writes changes back. At first this sounds doable but increased the complexity and maybe hurts performance a little..

After that I asked on IRC and I was told that there no apparent reason and it's just a missing feature. Hence I opened a feature request:

https://issues.dlang.org/show_bug.cgi?id=16187

(I don't feel strongly about this, so if either the performance or complexity increase turns out to be true, I will weep a bit, but survive).

@PetarKirov
Copy link
Member

PetarKirov commented Jun 26, 2016

No, @stonemaster is right - you can't do this without returning a proxy, which would be pessimization for the general case. The only that could help is something like opDispatchOpOpAssign(string member, string op), but for better or worse it doesn't exist.

@PetarKirov
Copy link
Member

BTW there several other interesting applications of bit manipulation that I didn't see mentioned:

  • using built-in integers for generating all possible subsets of a random access range of fixed size (e.g. 32 or 64)
  • using http://dlang.org/phobos/std_bitmanip#.BitArray to store a general set (of any size)
  • using http://dlang.org/phobos/std_bitmanip#.bitsSet to get a range of the indexes of the bits that were set, which useful when these indexes are used to index another array and you need to find the first element in it that passes a certain condition: allowed.bitsSet.countUntil!(idx => arr[idx].isOK)
  • using std.bitmanip : peak, read to parse any binary format (e.g. network packets, image formats, etc), especially useful when you have described the format with structs (i.e. your not reading it an int at a time).

In my experience bitFields is a more rarely used, but it's still a nice showcase of D's CT power.


As the compiler will add padding for variables with a size lower than the current
OS memory layout (`size_t.sizeof`) e.g. `bool`, `byte`, `char`, it is recommended
to start with fields of high alignments
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

. Missing at EOL

@wilzbach wilzbach force-pushed the bit_manipulations branch 2 times, most recently from a8b8640 to 60f09a4 Compare August 10, 2016 16:48

A generalization is to test for blocks that are longer than 1. Hence
a special read mask with the length of our block is needed
and the data block is shifted accordingly before applying our mask;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: instead of ;

@stonemaster stonemaster merged commit 1ca43a3 into dlang-tour:master Aug 10, 2016
@wilzbach wilzbach deleted the bit_manipulations branch August 13, 2016 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add gem: bitmanip
4 participants