Bit masking and Bitwise operation

Thread Starter

Parth786

Joined Jun 19, 2017
642
What is meaning of this line Data <<= 1 ? I don't understand what is meaning of this line in program ?
C:
unsigned char I2CSend(unsigned char Data)
  {
    unsigned char i, ack_bit;

    for (i = 0; i < 8; i++)
     {
        if ((Data & 0x80) == 0)
        {
            SDA = 0;
        }
       else
        {
           SDA  =  1;
           SCL  =  1;
           SCL  =  0;
           Data <<= 1;
        }
    }
}
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:

WBahn

Joined Mar 31, 2012
32,823
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.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
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.
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 ?
 

WBahn

Joined Mar 31, 2012
32,823
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".
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You really should be able to figure that out by now.
If you can't figure it out based on what you already know, look up "abbreviated assignment operators".
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 ?
 

WBahn

Joined Mar 31, 2012
32,823
Let me explain

Data<<=1 is same as Data = data << 1
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

Suppose data = 11001001

Data = 11001001 << 1

Data = 10010011

Is it right ? actually I don't understand this line in that routine ?
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.
 

WBahn

Joined Mar 31, 2012
32,823
I believe TS thinks << 1 means to left-shift a 1.

That's why TS is confused about the meaning of << 8.
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....
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Did you not read WBahn's response?

11001001

becomes

10010010
I have doubt. I think Your Example is wrong.
I wrote simple program to check value of data
C:
#include <stdio.h>
int main(void)
{
     int Data  =  60;

     Data  <<=  1;

     printf(" Value of Data  %d  \n", Data);

     return 0;
}
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:

JohnInTX

Joined Jun 26, 2012
4,787
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:

WBahn

Joined Mar 31, 2012
32,823
Your Data is an unsigned char in your original code but an int in your most recent code. Make up your mind.

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).
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
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 .
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)
C:
if (codition )
{
    // condition true //
    // Do this thing // 
}

else
{  
   // condition False //
   // Do this thing //
}
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)

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.
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

Joined Jun 26, 2012
4,787
((Data & 0x80) == 0)
let suppose Data = 10100000 and 0x80 = 10000000

Then
((10100000 & 10000000) == 0)

(10000000 == 0)
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.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
. 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.
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.
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
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:
http://academicearth.org/computer-science/
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.
 

MrSoftware

Joined Oct 29, 2013
2,273
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.
 
Top