Giter Site home page Giter Site logo

apaci / bithacks.h Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pkrumins/bithacks.h

1.0 2.0 0.0 69 KB

bithacks.h is a C header file containing useful bit manipulation macros

Home Page: http://www.catonmat.net/blog/bit-hacks-header-file/

bithacks.h's Introduction

This is "bithacks.h" C header file and it contains various macros for bit
manipulations. It's accompanied with "bithacks-test.c", which tests all the
macros.

It was written by Peteris Krumins ([email protected]).
His blog is at http://www.catonmat.net  --  good coders code, great reuse.

The code is licensed under the MIT license.

The code was written as a part of the article "bithacks.h - bit hacks header
file" on my website. The whole article can be read at:

    http://www.catonmat.net/blog/bit-hacks-header-file/

------------------------------------------------------------------------------

Table of contents:
    [1] Macros in bithacks.h file
    [2] The test program bithacks-test.c


[1]-Macros-in-bithhacks.h-file------------------------------------------------

The "bithacks.h" header provides the following macros:

    * B8(x)            - turns x written in binary into decimal,
    * B_EVEN(x)        - tests if x is even                    (bithack #1),
    * B_ODD(x)         - tests if x is odd         (inverse of (bithack #1)),
    * B_IS_SET(x, n)   - tests if n-th bit is set in x         (bithack #2),
    * B_SET(x, n)      - sets n-th bit in x                    (bithack #3),
    * B_UNSET(x, n)    - unsets n-th bit in x                  (bithack #4),
    * B_TOGGLE(x, n)   - toggles n-th bit in x                 (bithack #5),
    * B_TURNOFF_1(x)   - turns off the right-most 1-bit in x   (bithack #6),
    * B_ISOLATE_1(x)   - isolates the right-most 1-bit in x    (bithack #7),
    * B_PROPAGATE_1(x) - propagates the right-most 1-bit in x  (bithack #8),
    * B_ISOLATE_0(x)   - isolates the right-most 0-bit in x    (bithack #9),
    * B_TURNON_0(x)    - turn on the right-most 0-bit in x     (bithack #10).


[2]-The-test-program-bithacks-test.c------------------------------------------

The test program "bithacks-test.c" tests all the macros above.

You have to first compile it:

    $ gcc bit-hacks-test.c -o bithacks-test

This will create the "bithacks-test" program. Now run it to verify all the
bithacks in bithacks.h:

    $ ./bithacks-test
    All tests OK.


------------------------------------------------------------------------------

I explained all the bithacks in bithacks.h in another article "Bit Hacks You
Absolutely Must Know About":

    http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/

Also see the original article on bithacks.h "bithacks.h - bit hacks header
file":

    http://www.catonmat.net/blog/bit-hacks-header-file/


------------------------------------------------------------------------------

That's it. Happy bithacking!


Sincerely,
Peteris Krumins
http://www.catonmat.net

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.