Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Clarification on Bit Shifting

Status
Not open for further replies.

eebhoi01

Advanced Member level 4
Full Member level 1
Joined
Feb 22, 2012
Messages
116
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,347
Hello,

I am confused with the shifting of bits, please help me clarify my thoughts. If I do this code,

Capture.PNG

My questions is, by shifting SS, MOSI, SCK to the left by 1 isn't its value will now be
For SS: From 00010000 to 00100000
For MOSI: From 00100000 to 01000000
For SCK: From 10000000 to 00000000
Is this right? If I will apply "OR" with this values isn't it the final value of DDRB is this "01100000"?
If this is right, I believe this is wrong for what is suppose to be the value of DDRB which should be "10110000"

Thank you in advance.
 
Last edited:

What do you mean with "from xxx to xxx"? The shift is already applied to the left (from) value, why would you shift it a second time?

e.g. 1 << 0x4 = 0b00010000
 
What do you mean with "from xxx to xxx"? The shift is already applied to the left (from) value, why would you shift it a second time?

e.g. 1 << 0x4 = 0b00010000

I got sir, thanks. I was wrong on my initial value. Anyhow thank you so much.
 

I think what it is saying is:
DDRB=(1<SS)|(1<<MOSI)|(1<<SCK);
meaning:
DDRB becomes equal to a value of
'1' shifted 4 times = 00010000 (SS is equal to 4)
ORed with
'1' shifted 5 times = 00100000 (MOSI is equal to 5)
ORed with
'1' shifted 7 times = 10000000 (SCK is equal to 7)
so the final value is 10110000.

Basically, it is using the signal names to specify the position within DDRB that bits should be set.

Brian.
 

I think what it is saying is:
DDRB=(1<SS)|(1<<MOSI)|(1<<SCK);
meaning:
DDRB becomes equal to a value of
'1' shifted 4 times = 00010000 (SS is equal to 4)
ORed with
'1' shifted 5 times = 00100000 (MOSI is equal to 5)
ORed with
'1' shifted 7 times = 10000000 (SCK is equal to 7)
so the final value is 10110000.

Basically, it is using the signal names to specify the position within DDRB that bits should be set.

Brian.

Thank you sir, but the issue of my confusion is how I interpret the shift.
for A,,B. (I get error when typing greater and less sign, so i type comma instead)
I thought it was shifting B to left A times, but it is really shifting A to the left B times.
 

What is being shifted goes first then << then the number of times it is shifted.

Examples:
0x05 << 1 = 0x0A
0x05 << 2 = 0x14

In your original code the name of the bit is associated with it's position in the register using the #define lines. The value finally in DDRB is the ORed combination of a 1 shifted into each of the named bit positions. It is a very useful way to write code because it lets you use signal (or pin) names instead of just numbers. So if you want to SET (make = 1) a bit you use:
[variable or register] |= (1 << [name of the bit or pin])
and to CLEAR (make = 0) a bit you use:
[variable or register] &= ~(1 << [name of the bit or pin])

So in your original code, if you wanted to make a pulse on the SCK pin of RB, you could use:
RB |= (1 << SCK);
RB &= ~(1 << SCK);

Brian.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top