Maker Pro
Maker Pro

Bitwise AND assignment

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Hello

What is meaning of this data &= 0x0F. does it equal data = data & 0x0F ?
Code:
data &= 0x0F;
    
        for(i = 0; i < 4; i++)               
        {   
            data &=~( 0x80 >> i);   
                                      
            data |= 0x80>>i;
        }

data = 11101111
data = 11011111
data = 10111111
data = 01111111

data &= 0x0F

data 1110 1111
Ox0F 0000 1111
result 0000 1111
 
Last edited:

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
Yes correct, that operation clears the 4 left bits but retains the right 4 bits.
Thanks
Adam
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Yes correct, that operation clears the 4 left bits but retains the right 4 bits.
Thanks
Adam

How to solve this

data &=~( 0x80 >> i);

my attempt

0000 1111 ~ 0 0000 0000 1000 000
0000 0000 1111 0000


data &= means 0000 1111

( 0x80 >> i) means ox80 =0000 0000 1000 0000 >> 1 = 0 0000 0000 1000 000

I don't understand how does this operation work?
 
Last edited:

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
0x80 is the high bit only set in a byte. It is shifted right by some amount i, getting a selected single bit set in the byte. Then that is complemented, getting all other bits set. Finally, it is anded with the data. This retains all bits in the data except the one selected bit. This is used to selectively clear one bit in a data byte, without affecting any other bits.

Bob
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
data &= means 0000 1111

No it doesn't.

That's the assignment and an operation to be performed at the same time. It doesn't equal anything.

In algebra if I say x = 1 + 2, then I can say x = 3, but this is not algebra, this is an assignment.

( 0x80 >> i) means ox80 =0000 0000 1000 0000 >> 1 = 0 0000 0000 1000 000

You haven't done the bit shift.

Look for some examples of the >> operator until you know what it does.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
data = 1110 1111
0x0F = 0000 1111

data &= 0x0F

data = data & 0x0F
data = 1110 1111 & 0000 1111
data = 0000 1111

i = 0

data &=~( 0x80 >> i)

data &=~ (1000 0000 >> 0)
data &=~ (1000 0000)
data &= 0111 1111
data = data & 0111 1111
data = 0000 1111 & 0111 1111
data = 0000 1111

data |= 0x80>>i

data |= 1000 0000 >> 0
data |= 1000 0000
data = data | 1000 0000
data = 0000 1111 | 1000 0000
data = 1000 1111

Is it right ?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,722
Joined
Nov 17, 2011
Messages
13,722
This looks o.k. for i=0.
Let's see your solution for e.g. i=3 to verify you really got it.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
This looks o.k. for i=0.
Let's see your solution for e.g. i=3 to verify you really got it.
i = 3
Now data = 1000 1111

data &=~( 0x80 >> i)

data &=~ (1000 0000 >> 0000 0011)
data &=~ (0001 0000)
data &= 1110 1111
data = data & 1110 1111
data = 1000 1111 & 1110 1111
data = 1000 1111

data |= 0x80>>i

data |= 1000 0000 >> 0000 0011
data |= 0001 0000
data = data | 0001 0000
data = 1000 1111 | 1000 0000
data = 1000 1111
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,722
Joined
Nov 17, 2011
Messages
13,722
The first part (&=) is great.
In the second part you goofed. How do you arrive at this value? I assume it's a typographcal error only.
data = 1000 1111 | 1000 0000
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
The first part (&=) is great.
In the second part you goofed. How do you arrive at this value? I assume it's a typographcal error only.

OK I am telling you what I want. I was trying to understand the program in this link https://www.8051projects.net/keypad-interfacing/8051-programming.php

I don't have much understanding about whole program so that's why I am trying to understand in small part

Code:
#include <AT89X51.H>   //Include file for 8051

#define keyport P2    //keypad connected to P2
#define col1 P2_0    //column 1
#define col2 P2_1    //column 2
#define col3 P2_2    //column 3
#define col4 P2_3    //column 4

#define TRUE 1        //some defines
#define FALSE 0

void key_init(){
    keyport &= 0x0F;    //make Rows as o/p and cols are i/p
}

unsigned char get_key(){
 
unsigned char i, k, key = 0;
    k = 1;
 
   for( i = 0; i < 4; i++){                   //loop for 4 rows
        keyport  &=~ (0x80 >> i );           //to make rows low 1 by 1
            if(!col1){                          //check if key1 is pressed
                key = k+0;                  //set key number
                while(!col1);              //wait for release
                return key;                 //return key number
            }
            if(!col2){                      //check if key2 is pressed
                key = k+1;              //set key number
                while(!col2);           //wait for release
                return key;            //return key number
            }
            if(!col3){                   //check if key3 is pressed
                key = k+2;           //set key number
                while(!col3);        //wait for release
                return key;          //return key number
            }
            if(!col4){                //check if key4 is pressed
                key = k+3;           //set key number
                while(!col4);         //wait for release
                return key;          //return key number
            }
        k+=4;                         //next row key number
        keyport |= 0x80>>i;    //make the row high again
    }
    return FALSE;            //return false if no key pressed
}
 
Last edited:
Top