Noob question: why equ 1<<0 in barebones tutorial
Noob question: why equ 1<<0 in barebones tutorial
I am so sorry about this question, but I can't find any answer and I don't get it.
In the barebones (which I have got working from a usb memory stick with Grub2 - hurrah!) there are the first lines that go
MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries
MEMINFO equ 1<<1
Now I know that << means shift left, but since the first one means 1 shifted left by 0 places then isn't it just 1, so why not just write that.
And 1<<1 means 10 which is 2 so why not just write that?
And if you care to explain how this aligns the code on a page boundary I'd be pleased
Gordon
In the barebones (which I have got working from a usb memory stick with Grub2 - hurrah!) there are the first lines that go
MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries
MEMINFO equ 1<<1
Now I know that << means shift left, but since the first one means 1 shifted left by 0 places then isn't it just 1, so why not just write that.
And 1<<1 means 10 which is 2 so why not just write that?
And if you care to explain how this aligns the code on a page boundary I'd be pleased
Gordon
Re: Noob question: why equ 1<<0 in barebones tutorial
And if you need to set 21-st bit, why not just write 2097152?gfmoore wrote:Now I know that << means shift left, but since the first one means 1 shifted left by 0 places then isn't it just 1, so why not just write that.
And 1<<1 means 10 which is 2 so why not just write that?
There's no need to pre-calculate constants, it will take few microseconds for compiler to do that. And writing 1<<0, 1<<1 or 1<<21 your code clearly says "A value with its 0-th, 1-st or 21-st bit set" not "some magic number".
If something looks overcomplicated, most likely it is.
Re: Noob question: why equ 1<<0 in barebones tutorial
It's just common practice:
And so on.
Code: Select all
1 << 0 # Bit #0
1 << 1 # Bit #1
1 << 2 # Bit #2
Every good solution is obvious once you've found it.
Re: Noob question: why equ 1<<0 in barebones tutorial
It's ugly. I'd use hex or binary notation instead.
Re: Noob question: why equ 1<<0 in barebones tutorial
it is ugly, but it is also clear.
For instance (1 << 4) --> The fourth bit, rather than 16 which could be being used for all manner of things. It is some attempt to communicate contextual information, speeding the processes of a programmer comprehending both what, how and why a piece of code is doing what it does.
Similar to comments really.
For instance (1 << 4) --> The fourth bit, rather than 16 which could be being used for all manner of things. It is some attempt to communicate contextual information, speeding the processes of a programmer comprehending both what, how and why a piece of code is doing what it does.
Similar to comments really.
Re: Noob question: why equ 1<<0 in barebones tutorial
1 << 4 would be 0x10. It is clear. I'd only use bitshifts if the shifts themselves are subject to possible change:SDS wrote:it is ugly, but it is also clear.
For instance (1 << 4) --> The fourth bit, rather than 16 which could be being used for all manner of things. It is some attempt to communicate contextual information, speeding the processes of a programmer comprehending both what, how and why a piece of code is doing what it does.
Similar to comments really.
Code: Select all
#define SOME_BIT 4
...
if (bit & (1 << SOME_BIT))
Code: Select all
#define MASK 0x10
...
if (bit & MASK)
Re: Noob question: why equ 1<<0 in barebones tutorial
You might do that. Other people do different.
The idea behind "contextual information" is that 0x10 is just a number, while 1 << 4 is an integer with (only) bit #4 set. The integer is the same, but 1 << 4 makes the intent explicit.
But people are still resisting the idea of code comments, so who am I to judge.
The idea behind "contextual information" is that 0x10 is just a number, while 1 << 4 is an integer with (only) bit #4 set. The integer is the same, but 1 << 4 makes the intent explicit.
But people are still resisting the idea of code comments, so who am I to judge.
Every good solution is obvious once you've found it.
Re: Noob question: why equ 1<<0 in barebones tutorial
I prefer documenting few portions of code that carries complex logic behind it and for which inline commenting renders it as an essay.Solar wrote:But people are still resisting the idea of code comments
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
Re: Noob question: why equ 1<<0 in barebones tutorial
Phew I didn't get my head bitten off - I was so worried (lol)
Thanks guys and now it is clear as to why it is done that way.
As Solar says: Every good solution is obvious once you've found it.
Thanks guys and now it is clear as to why it is done that way.
As Solar says: Every good solution is obvious once you've found it.
Re: Noob question: why equ 1<<0 in barebones tutorial
What about this then:
would you do it like this:
I find this type of notation kind of "reinventing the wheel". The compiler already has ways to provide bitmasks. At least my PIC compiler can do this:
or
I would not want to change the last example to:
As it is totally unreadable!
Code: Select all
#define ACTION_MASK 0x180
...
if (Msg & ACTION_MASK)
Code: Select all
#define ACTION_MASK (1 << 8) | (1 << 7)
...
if (Msg & ACTION_MASK)
Code: Select all
movlw b'0001000'
Code: Select all
movlw b'1001010'
Code: Select all
movlw (1 << 7) | (1 << 4) | (1 << 2)
Re: Noob question: why equ 1<<0 in barebones tutorial
And incorrect. It should berdos wrote:I would not want to change the last example to:Code: Select all
movlw b'1001010'
As it is totally unreadable!Code: Select all
movlw (1 << 7) | (1 << 4) | (1 << 2)
Code: Select all
movlw (1 << 6) | (1 << 3) | (1 << 1)
If a trainstation is where trains stop, what is a workstation ?
Re: Noob question: why equ 1<<0 in barebones tutorial
So, you would preferrdos wrote:I would not want to change the last example to:Code: Select all
movlw b'1001010'
As it is totally unreadable!Code: Select all
movlw (1 << 7) | (1 << 4) | (1 << 2)
Code: Select all
and $0b1111111111111111111111111111111111111111111111101011111011111111, %rax
Code: Select all
and $~(1 << 8 | 1 << 14 | 1 << 16), %rax
If something looks overcomplicated, most likely it is.
Re: Noob question: why equ 1<<0 in barebones tutorial
To be honest, I wouldn't particularly care. That said, if it was the specific bits which were important I would probably use the latter.rdos wrote:Code: Select all
#define ACTION_MASK 0x180
Code: Select all
#define ACTION_MASK (1 << 8) | (1 << 7)
As you mentioned, bitmasks work fine. However, if you have set, say the 47th bit in a 64bit integer, then then I would argue that
Code: Select all
1 << 47
Code: Select all
b'100000000000000000000000000000000000000000000000'
800000000000
140737488355328
Once I have a working general solution, I see little reason to use a less general solution for simple cases. It doesn't make things more clear to represent them as a number after all.
Re: Noob question: why equ 1<<0 in barebones tutorial
Assuming that normal good practice is used ... such asberkus wrote:If it is in C, then it's very error prone the way you wrote it.
Code: Select all
#define ACTION_MASK ((1 << 8) | (1 << 7))
Re: Noob question: why equ 1<<0 in barebones tutorial
I must admit that is what I would do. If it was some obscure bit like 27 there might be some point in doing it the other way, but up to about 0x200 I would just write 0x200.gfmoore wrote:then isn't it just 1, so why not just write that.
And 1<<1 means 10 which is 2 so why not just write that?