Shop OBEX P1 Docs P2 Docs Learn Events
C Bit Masking And Bit Shifting — Parallax Forums

C Bit Masking And Bit Shifting

idbruceidbruce Posts: 6,197
edited 2015-03-14 02:08 in General Discussion
While programming in MFC, I would occassionally use masks to to add or subtract various program options, but I can't ever really remember a time when I needed to do any shifting. Since becoming interested in microcontrollers, I have noticed that there is a great need to understand and use all the various bit shifting and bit masking techniques, but I have not yet taken the time to learn and understand these little buggers. I have finally come to the realization that I must take this time out and learn this stuff for various reasons. For one, it makes me feel very ignorant as a programmer :(

I have all kinds of documentation on the various mask and shifts, in a wide variety of programming languages, but out of all the documentation that I have, there is no complete description, with examples about how to use the various masks and shifts successfully. However I must admit that the Propeller Manual does a pretty good job of laying it out for SPIN.

What I really need is some good documentation for the C language. Does anybody have any ideas where I could find extensive documentation and examples of bit shifts and bit masks for C?

EDIT: In my quest for new knowledge, I came across this nice reference: http://graphics.stanford.edu/~seander/bithacks.html

Comments

  • Heater.Heater. Posts: 21,230
    edited 2015-03-14 01:01
    I love that bithacks page. Have referred to for years now. If you understand what is on that page then you are good to go.

    One thing lacking from that page and annoyingly not available in C is rotating bits. I always thought that was a bit odd as C is a "down to the metal" kind of language and many processors hare rotate instructions.

    Still it's easy enough to do:
    int leftRotate(int n, unsigned int d)
    {
       return (n << d)|(n >> (INT_BITS - d));
    }
     
    int rightRotate(int n, unsigned int d)
    {
       return (n >> d)|(n << (INT_BITS - d));
    }
    

    Be careful with right shifting. The >> operator will get you different bit patterns depending on the type being shifted. Singed types (int) will keep their sign. Negative numbers shifted right will have their sign bit replicated into the top bits. You may need to be careful and cast to an unsigned type before shifting so that zeros get shifted in at the top.
  • idbruceidbruce Posts: 6,197
    edited 2015-03-14 02:08
    Heater
    I love that bithacks page. Have referred to for years now. If you understand what is on that page then you are good to go.

    Yea it appears to be a very nice and handy reference. As for understanding it, well that is quite a different story. I never got my feet wet with that kind of programming, but I am starting to dig in deep now and fill some of voids of the stuff I failed to learn over the years. It is a completely different ball game programming in MFC or C++ and working with mostly GUI type of stuff. Sure there was math involved, but a lot of this stuff, I just never had to deal with it.

    Thanks for the tips on shifting.
Sign In or Register to comment.