Out of interest why is there so much use of shift left (<

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The syntax "(1 << X)" is used extensively thought out AVR code and many other platform examples.

I'm just interested why.

IIUIC if X is a constant the shift left will get optimised out by the compiler to a number and will never be executed as a shift left instruction on the CPU. So why isn't it just a constant rather than a shift left constant ?

Consider this common pseudo code:

#define BIT6 6
[...]
PORTB = (1 << BIT6);

Why isn't it usual to see:

#define BIT6 32      -- or 0b00100000 of you prefer
[...]
PORTB = BIT6;

To me the latter its easier to read. I'm just intrigued where the use of "(1 << X)" originated from ?

Jon.

Jon Russell

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

To me the latter its easier to read.

Well, to many the former is the easier thing to read. If you are talking about the sixth bit, it sure seems more obvious to actually have the digit "6" in there somewhere.

The technique gains momentum when ussed together with the bit numbers for the peripheral registers that are in pre-generated header files.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The standard register bit references in are bit numbers like

#define    TheBit     6

To use the standard bit name, you need some mechanism to generate a 1 in the correct bit position. That is the reason.

Of course, after it is all compiled, it becomes an ordinary number, but it is readability in the source code that matters, here.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

0b____ isn't standard, though quite common. The purists might object to that.

If you follow the conventions that Atmel is using for the Xmega (and I have no idea of the origin), then you can have it either way--there are _bm and _bp created, for "bit mask" and "bit position".

Then there are those like Johan and Jim above that are like the right-wing radio talk show hosts and have decided that if they say the same thing often enough and loud enough then the masses will consider it to be an axiom and the basis of all faith and science. Kind of a "1984" situation. It isn't PC for me to say "four", when the Right-Way Police have decided that I must say "the cube root of sixteen squared".

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If you follow the conventions that Atmel is using

But this is exactly why the << construct is in use common in avr-gcc: it comes from Atmel's convention of defining bit numbers, not bit masks.

Quote:
there are _bm and _bp created, for "bit mask" and "bit position".

And if you look up the _bm defines, many are defined using <<, though for some reason they seem quite inconsistent about it. And for some odd reason they didn't use the corresponding _bp define in the _bm define. I would think that that would have the potential of creating numerous errors.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But this is exactly why the << construct is in use common in avr-gcc: it comes from Atmel's convention of defining bit numbers, not bit masks.

Ummm--quote the rest of the sentence, too.

But you are correct, upon re-reading. I should have said "naming convention" in there somewhere. Of course (or "as expected") the underlying uses the bit names and <<. But now there are the _bm/_bp to help identify whether the FROG bit is the 4 _bp or (1<<4) _bm.

So now we have mixed-case plus shifted characters when doing code. How about the good old days when fumbling two-fingered old typists could just say char and int without reaching all over the keyboard?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Right, and you could even load the program from punched tape so you did not have to retype it by hand every time!

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Right, and you could even load the program from punched tape so you did not have to retype it by hand (on a teletype) every time!

Jim

Don't know how this got in the thread and how to get rid of it!

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sat. Feb 6, 2010 - 08:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

ka7ehk wrote: [...]

You sure did, Jim! :D

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
How about the good old days when fumbling two-fingered old typists could just say char and int without reaching all over the keyboard?

You can go back to using a horse and buggy as well, but people are going to get mad at the crap you leave behind :)

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's a communist plot! It should a right shift....

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
How about the good old days when fumbling two-fingered old typists could just say char and int without reaching all over the keyboard?

Lucky C doesn't have "asdf" and "zxcv" as fundamental types.

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
And if you look up the _bm defines, many are defined using <<, though for some reason they seem quite inconsistent about it. And for some odd reason they didn't use the corresponding _bp define in the _bm define. I would think that that would have the potential of creating numerous errors.

I have not looked in detail but I expect these files are auto generates from a comon source that will feed into the hardware disign, the simulator, test suites and documentation. We used this methodology where I used to work and we did not have to deal with anthing like the number of models Amtel have just 2 or 3 variations at a time. I expect the 'inconsistancy' will actualy be very consiustant.