# Bit masking and Bitwise operation

Discussion in 'Programmer's Corner' started by Parth786, Oct 24, 2017.

1. ### Parth786 Thread Starter Active Member

Jun 19, 2017
642
45

What is meaning of this line Data <<= 1 ? I don't understand what is meaning of this line in program ?
Code (C):
1. unsigned char I2CSend(unsigned char Data)
2.   {
3.     unsigned char i, ack_bit;
4.
5.     for (i = 0; i < 8; i++)
6.      {
7.         if ((Data & 0x80) == 0)
8.         {
9.             SDA = 0;
10.         }
11.        else
12.         {
13.            SDA  =  1;
14.            SCL  =  1;
15.            SCL  =  0;
16.            Data <<= 1;
17.         }
18.     }
19. }
My understanding

Example of Bitwise Logic Operator

Bitwise NOT : Reverses each bit in its operand.
For example:- A = ~ A

00110110 = ~11001001

Bitwise AND : If either or both bits are 0, the result is 0.
For example:- C = A & B

11001001 & 10011011 = 10001001

Bitwise OR : If either or both bits are 1, the result is 1.
For example:- C = A I B

11001001 | 10011011 = 11011011

Example of Bitwise Shift Operator

Bitwise left shift For Example :- 11001001 << 1 = 10010011

Bitwise right shift For Example :-11001001 >> 1 = 11100100

Note : I have edited code

Last edited: Oct 24, 2017
2. ### WBahn Moderator

Mar 31, 2012
23,587
7,216
Left shift always shifts a zero into the rightmost bit.

Right shift does sign extension by retaining the leftmost bit unchanged if the operand is a signed data type; this is known as an arithmetic right shift. If the operand is an unsigned type, a zero is shifted in to the leftmost bit; this is known as a logical right shift.

MrSoftware likes this.
3. ### Parth786 Thread Starter Active Member

Jun 19, 2017
642
45
Look at this line Data<<=1. There re two operator
suppose data is 8 bits Data = 1111 0011
Then 1111 0011 << = 1
What happen here ?

what is meaning of this1111 0011 << = 8 ?

4. ### WBahn Moderator

Mar 31, 2012
23,587
7,216
You really should be able to figure that out by now.

What is the following:

x += 10;
y *= 5;

Use what you know about that to think what

z <<= 4;

almost certainly has to mean.

Then consider what

a &&= b;
a |= 3;

mean.

If you can't figure it out based on what you already know, look up "abbreviated assignment operators".

Parth786 likes this.
5. ### Parth786 Thread Starter Active Member

Jun 19, 2017
642
45
Let me explain

Data<<=1 is same as Data = data << 1

Suppose data = 11001001

Data = 11001001 << 1

Data = 10010011

Is it right ? actually I don't understand this line in that routine ?

6. ### MrChips Moderator

Oct 2, 2009
17,788
5,550
Did you not read WBahn's response?

11001001

becomes

10010010

7. ### WBahn Moderator

Mar 31, 2012
23,587
7,216
Close, but not quite. Remember, C (and most programming languages) are case sensitive, so data and Data are two completely unrelated identifiers.

Data <<= 1 is the same as Data = Data << 1

No, it is not right. I was very explicit about stating that a zero is shifted into the rightmost bit on a left shift operation.

8. ### WBahn Moderator

Mar 31, 2012
23,587
7,216
It might help if you were to properly format (indent) the code. Why do you steadfastly refuse to do that?

9. ### MrChips Moderator

Oct 2, 2009
17,788
5,550
I believe TS thinks << 1 means to left-shift a 1.

That's why TS is confused about the meaning of << 8.

10. ### WBahn Moderator

Mar 31, 2012
23,587
7,216
That's possible. Of course, Googling something like "left shift in C" would go a long way to prevent such an interpretation, but when your primary means of trying to learn something is to expect others to explain everything to you....

panic mode likes this.
11. ### Parth786 Thread Starter Active Member

Jun 19, 2017
642
45
I have doubt. I think Your Example is wrong.
I wrote simple program to check value of data
Code (C):
1. #include <stdio.h>
2. int main(void)
3. {
4.      int Data  =  60;
5.
6.      Data  <<=  1;
7.
8.      printf(" Value of Data  %d  \n", Data);
9.
10.      return 0;
11. }
Result
Data = Decimal 60 = Binary 00111100
Data = Data << 1
Data = Decimal 120 = Binary 01111000

Example 2
if Data = 80 = 01010000
Data = Data << 1
160 = 10100000

Last edited: Oct 24, 2017
12. ### JohnInTX Moderator

Jun 26, 2012
3,413
1,735
The example is correct and is consistent with your test code.
The << x operator shifts the register x bits to the left. Zeros are shifted into the LSbit(s) and the MSbit(s) are lost.
To answer your original question, shifting is used to output the 8 bits of the register one bit at a time. After each shift, the next bit is in the Most Significant Bit of the register. ANDing the register with 10000000 results in 0 if the MS bit was 0 and 1 if the MSbit was 1. That result is used to set or clear SDA to output that data bit. The process is repeated 8 times for the 8 bit byte.

That’s as simple as I can make it. You should load up your code in Proteus and step it through the shift loop while examining the register bits as they are shifted and the result of the AND operation. How it works will become clear and take less time than posting here.

I know you have access to a full featured simulator and debugger. It is time to learn how to use them. you should also be consulting a C text - K&R is available online for free - to learn how C operators work.

Last edited: Oct 24, 2017
Parth786 likes this.
13. ### WBahn Moderator

Mar 31, 2012
23,587
7,216

Make Data an unsigned char. Now set it equal to 0xC9 (which is 11001001, or decimal value 201).

Now execute

Data <<= 1;

and print out the result as an unsigned integer and you should get 0x92 (which is 10010010, or decimal value 146).

14. ### Parth786 Thread Starter Active Member

Jun 19, 2017
642
45
You are right Now I understood what the meaning of left shift assign operator.

For example 00111100 become 01111000 and 01010000 become 10100000

We are checking this condition ((Data & 0x80) == 0)
Code (C):
1. if (codition )
2. {
3.     // condition true //
4.     // Do this thing //
5. }
6.
7. else
8. {
9.    // condition False //
10.    // Do this thing //
11. }
I understand how does this work but I don't understand what the real use of this condition in program. What happen when it true and what happen when it false ?

((Data & 0x80) == 0)
let suppose Data = 10100000 and 0x80 = 10000000

Then
((10100000 & 10000000) == 0)

(10000000 == 0)

I have some issues regarding proteus. and I am trying to fix them. If I use old version of proteus I can test device with no debug access. so I downloaded latest version but it doesn't allow me to save and check my file. I am trying to install crack version. So that I can debug my code . I have tried many version but still didn't get success. I am working on it and soon I will test program by debugging code.

JohnInTX likes this.
15. ### JohnInTX Moderator

Jun 26, 2012
3,413
1,735
No,
(10100000 & 10000000) = 10000000, a NON-ZERO number i.e. TRUE in C.
Each bit is ANDed with the corresponding bit in the other argument. If corresponding bits are BOTH '1', the result of the AND operation is not zero. In this case, that means that the MSbit of the data register is '1' and you set SDA to a '1'. If the MS bit was '0' then the result of the AND would be 0 (FALSE in C) and you clear SDA to '0'.

This is for one bit of the 8 bit data. For the next bit you shift the data left one place and repeat:
(01000000 & 10000000) = 00000000, a ZERO i.e. FALSE in C. So SDA is cleared to '0' for this bit.

You test the result of the AND with the 'if' statement shown in #14 and update SDA there.

The reason you shift is to look at each individual data bit in order to send each of them one by one.

BTW the code you show in #1 is incorrect. It only clocks when the data is '1'. Sloppy.

Parth786 likes this.
16. ### Parth786 Thread Starter Active Member

Jun 19, 2017
642
45
I have downloaded - K&R and I am reading this book. That's why I created this thread to understand bitwise operator. I am practicing with c compiler. Most of the problem I have solved myself

I am not understanding That I am on the right track or not. because I have written some program for embedded system. also I have written some c programming. I have started learning both subject but how to learn in sequence.

It would be very useful for me if someone guide me on right direction. I have been working with you for few month's. You know my weakness I have little bit knowledge in both C and embedded C programming. I have been written program for both. I am following your advice. I have started to make flow chart. I will take care of proper code indenting and I will use debug features to test code.

I want to learn with you in sequence. sequence mean complete one topic then another topic. sometime I moved from one topic to another topic. I can understand that you have to less time. I understand I am not only one who need your help. there are many people like me who need help. I just want to know how to start and which topic should be complete first. simple I want to make list or index page so that I can understand what to do first and what to do next.

17. ### JohnInTX Moderator

Jun 26, 2012
3,413
1,735
What you are asking is what you would get with a well designed course on C and another in programming concepts. As I have said on the boards and in PM, a forum is not the place to get a formal education in C for the reasons noted in PM. Some of your problems are still in basic programming skills - figuring out a solution to a problem and documenting it. This thread is more about learning the C language. Both are important.

I would recommend you find a good online tutorial with a list of topics and follow them one by one. Here are a few for C but there are many others. Find one that you like and stick with it.
https://www.tutorialspoint.com/cprogramming/index.htm
http://www.learn-c.org/
https://www.cprogramming.com/

Unfortunately, those are mostly about the language and not about programming as problem solving. Look for things like this that are more about the process and less about the language:
https://en.wikiversity.org/wiki/Introduction_to_Programming

There are lots of online lectures if you have the time from guys like:
https://ocw.mit.edu/courses/

As far as conducting a 1 on 1 course on the forums, that would not be a practical solution, sorry.

panic mode, xox and Parth786 like this.
18. ### MrSoftware Senior Member

Oct 29, 2013
1,241
366
You can learn bit shifting on any processor. Get yourself a copy of MicrosoftVisual Studio, there is a free version and it is a really excellent IDE and debugger. Write some bit shifting code. Open the memory watch and/or the variable watch for your variables. Single step over the code and watch what happens to the memory.

JohnInTX likes this.
19. ### be80be AAC Fanatic!

Jul 5, 2008
1,710
338
Parth786 likes this.