0% found this document useful (0 votes)
4 views

P4E Lecture 10 - Low Level Bit Programming

The document is a lesson plan on bit operations in programming, specifically focusing on binary and hexadecimal conversions, bit manipulation, and bit masking techniques. It covers various bitwise operators, their precedence, and practical examples of setting, clearing, and testing bits. Additionally, it includes a sample program demonstrating bit manipulation using a loop to shift bits for LED control.

Uploaded by

coolguysemail222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

P4E Lecture 10 - Low Level Bit Programming

The document is a lesson plan on bit operations in programming, specifically focusing on binary and hexadecimal conversions, bit manipulation, and bit masking techniques. It covers various bitwise operators, their precedence, and practical examples of setting, clearing, and testing bits. Additionally, it includes a sample program demonstrating bit manipulation using a loop to shift bits for LED control.

Uploaded by

coolguysemail222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Further Programming

University of the
Concepts – Bit operations
West of England Programming for Engineers
LESSON PLAN K. Emrith @
UWE2019

• Binary and Hex

• Bit Manipulation
– Bit Manipulation Operators
– Compound Assignments
– Bit Masking
Binary to decimal conversion K. Emrith @
UWE2019

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1 0 0 0 0 0 1 1
128+2+1 = 131
Binary to hexadecimal conversion K. Emrith @
UWE2019

Convert chunks of 4 bits at a


time
23 22 21 20

8 4 2 1
1 1 1 0
14 in decimal => E

[0 1 2 3 4 5 6 7 8 9 A B C D E
F]
Declaring Binary or Hex values K. Emrith @
UWE2019

X = 15; //decimal
X = 0xF; //Hex
X = 0b1111; //binary

printf(“%d”,X); //all give the same output


Bit Manipulation K. Emrith @
UWE2019

C has many bit-manipulation operators:


1. & Bit-wise AND
2. | Bit-wise OR
3. ^ Bit-wise XOR
4. ~ Negate (one’s complement)
5. >> Right-shift
6. << Left-shift
• Plus assignment versions of each (compound
assignments).
Bitwise Shift Operators K. Emrith @
UWE2019

• The bitwise shift operators shift the bits in an


integer to the left or right:
<< left shift
>> right shift
• The operands for << and >> may be of any
integer type (including char).
• The integer promotions are performed on both
operands; the result has the type of the left
operand after promotion.
Bitwise Shift Operators … Examples K. Emrith @
UWE2019

• To modify a variable by shifting its bits, use the


compound assignment operators <<= and
>>=:
i = 13;
/* i is now 13 (binary 0000000000001101) */
i <<= 2; //same as i = i << 2;
/* i is now 52 (binary 0000000000110100) */
i >>= 2;
/* i is now 13 (binary 0000000000001101) */
Bitwise Complement, And, K. Emrith @
UWE2019

Exclusive Or, and Inclusive Or


• There are four additional bitwise operators:
~ bitwise complement
& bitwise and
^ bitwise exclusive or
| bitwise inclusive or
• The ~ operator is unary; the integer
promotions are performed on its operand.
• The other operators are binary; the usual
arithmetic conversions are performed on
their operands.
Operator Precedence K. Emrith @
UWE2019

• Each of the ~, &, ^, and | operators has a


different precedence:
Highest: ~
&
^
Lowest: |
• Examples:
i & ~j | k means (i & (~j)) | k
i ^ j & ~k means i ^ (j & (~k))
• Using parentheses helps avoid confusion.
Compound Operators K. Emrith @
UWE2019

• The compound assignment operators &=, ^=, and |=


correspond to the bitwise operators &, ^, and |:
i = 21;
/* i is now 21 (binary 0000000000010101) */
j = 56;
/* j is now 56 (binary 0000000000111000) */
i &= j; // i= i & j
/* i is now 16 (binary 0000000000010000) */
i ^= j; // i= i ^ j
/* i is now 40 (binary 0000000000101000) */
i |= j; // i= i | j
/* i is now 56 (binary 0000000000111000) */
Bit Masking K. Emrith @
UWE2019

• The bitwise operators can be used to extract or


modify data stored in a small number of bits (bit
masking operations).
• Common single-bit operations:
– Setting a bit
– Clearing a bit
– Testing a bit
• Assumptions:
– i is a 16-bit unsigned short variable.
– The leftmost—or most significant—bit is numbered 15
and the least significant is numbered 0.
Bit Masking – Setting bits K. Emrith @
UWE2019

• Setting a bit. The easiest way to set bit 4 of i is


to or the value of i with the constant 0x0010:
i = 0x0000;
/* i is now 0000000000000000 */
i |= 0x0010;
/* i is now 0000000000010000 */
• If the position of the bit is stored in the variable
j, a shift operator can be used to create the
mask:
i |= 1 << j; /* sets bit j */
• Example: If j has the value 3, then 1 << j is
0x0008.
Bit Masking – Clearing bits K. Emrith @
UWE2019

• Clearing a bit. Clearing bit 4 of i requires a mask


with a 0 bit in position 4 and 1 bits everywhere
else:
i = 0x00ff;
/* i is now 0000000011111111 */
i &= ~0x0010;
/* i is now 0000000011101111 */
• A statement that clears a bit whose position is
stored in a variable:
i &= ~(1 << j); /* clears bit j */
Bit Masking – Testing bits K. Emrith @
UWE2019

• Testing a bit. An if statement that tests


whether bit 4 of i is set:
if (i & 0x0010) … /* tests bit 4 */
• A statement that tests whether bit j is set:
if (i & 1 << j) … /* tests bit j */
Modifying bit-fields K. Emrith @
UWE2019

• Modifying a bit-field requires two


operations:
– A bitwise and (to clear the bit-field)
– A bitwise or (to store new bits in the bit-field)
• Example:
i = i & ~0x0070 | 0x0050;
/* stores 101 in bits 4-6 */
• The & operator clears bits 4–6 of i; the |
operator then sets bits 6 and 4.
Accessing Bit-Fields K. Emrith @
UWE2019

• To generalize the example, assume that j


contains the value to be stored in bits 4–6 of i.
• j will need to be shifted into position before the
bitwise or is performed:
i = (i & ~0x0070) | (j << 4);
/* stores j in bits 4-6 */
• The | operator has lower precedence than & and
<<, so the parentheses can be dropped:
i = i & ~0x0070 | j << 4;
Accessing Bit-Fields K. Emrith @
UWE2019

• Retrieving a bit-field. Fetching a bit-field at the


right end of a number (in the least significant
bits) is easy:
j = i & 0x0007;
/* retrieves bits 0-2 */
• If the bit-field isn’t at the right end of i, we can
first shift the bit-field to the end before
extracting the field using the & operator:
j = (i >> 4) & 0x0007;
/* retrieves bits 4-6 */
K. Emrith @
UWE2019

~ 1001  0110

1001 1011  0x9B

1 << J  0X0001 << J


0X0001  0001 0000

~0X0070  0XFF8F  1111 1111 1000 1111


Sample program K. Emrith @
UWE2019

int alt_main (void){


alt_u8 led = 0x2;
alt_u8 dir = 0;
volatile int i;

/* Infinitely shift a variable with one bit set back and forth, and write it to the LED PIO. Software
loop provides delay element. See if you can understand it, it will be great revision!*/

while (1){
if (led & 0x9){ dir = (dir ^ 0x1);}
if (dir){ led = led >> 1;}
else{ led = led << 1; }
IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, led);
i = 0;
while (i<200000)
i++;
} //end while
return 0;
}

You might also like