Recently, I have realized that no matter how much C, C++ or other high level languages you know it all comes down to bit and bytes view of program.
I started realizing that after all it’s not that easy to actually think and write in bit/byte manipulation. This is my attempt to learn and be more comfortable about thinking in bit/bytes.
Table of Contents
- Basics
- Bit Fields
- Problems
- How set a single bit in a byte?
- How to unset single bit in a byte?
- One function to set and unset
- How to unset range of bits?
- How to set range of bits?
Basics
Inroduction link
0 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0 || | | | || |+- bit 31 | | | bit 0 -+| | | | | | +-- BYTE 3 -----+--- BYTE 2 ----+--- BYTE 1 ----+-- BYTE 0 -----+ | | | +----------- WORD 1 ------------+----------- WORD 0 ------------+ | | +--------------------------- DWORD -----------------------------+
Hexadecimal Numbers 0 1 2 3 4 5 6 7 8 9 A B C D E F
Bitwise Operators The & operator (AND) 1 & 1 == 1 1 & 0 == 0 0 & 1 == 0 0 & 0 == 0 The | operator (OR) 1 | 1 == 1 1 | 0 == 1 0 | 1 == 1 0 | 0 == 0 The ^ operator (XOR) 1 ^ 1 == 0 1 ^ 0 == 1 0 ^ 1 == 1 0 ^ 0 == 0 The ~ operator The ~ (Ones Complement or inversion) operator acts only on one value and it inverts it. The >> (Right Shift) 00001100 - b 00110000 - b << 2 The << (Left Shift) 00001100 - b 00000011 - b >> 2 Another example is 1<<4; 0001 0000
Bit Fields
struct date_struct {
BYTE day : 5, // 1 to 31
month : 4, // 1 to 12
year : 14; // 0 to 9999
} date
|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|
| | | |
+------ year ---------------+ month +-- day --+
date.day = 12;
dateptr = &date;
dateptr->year = 1852;
Basics of Binary numbers and operations (link)
Problems
How set a single bit in a byte?
For e.g In byte 0000 1000 set bit no 6 will produce 0100 1000
(Remember bit number starts with 0-7)
(Remember bit number starts with 0-7)
//For problems where certain bit values needs to be changed, first we
//need to create a bit mask.
//Bit mask is a temporary variable with some value. Using this value
//we will access and change specific bits in a byte of data.
//For e.g.
//To set 6th bit in a byte 0000 1000
//We have MASK 0100 0000 (OR)
// ——————————————
// 0100 1000
//To turn on certain bit in a byte (OR) is used.
int set_bit(int val, int num, bool bitval)
{
return (val | (bitval << num));
}
//Here, val = 0000 1000 = 8
// num = 6 (set 6th bit)
// bitval = 1 (set to 1)
// 0000 1000
//(OR) 0100 0000 (1 << 6)
// ————————-
// 0100 0000
How to unset single bit in a byte?
For e.g In byte 0100 1000 unset bit no 6 will produce 0000 1000
(Remember bit number starts with 0-7)
//To unset specific bit we will use (AND) operation.
//Mask value need to be ‘0’ for the bit to unset but rest of the bits
//need to ‘1’. The reason for rest of the bits to set as ‘1’ is as
//we are doing (AND), we don’t want to unset other bits which are
//already set.
//
//For e.g.
//To unset 6th bit 0100 1000
// MASK 1011 1111 (AND)
// ——————————-
// 0000 1000 (Result)
int unset_bit(int val, int num, bool bitval)
{
return (val & ~(bitval << num));
}
//Here, val = 0100 1000
// num = 6
// bitval = 0
// (bitval << num) = 0100 0000
// ~(bitval << num) = 1011 1111
//
// 0100 1000
// 1011 1111 (AND)
// —————————-
// 0000 1000 (Result)
One function to set and unset
//To unset specific bit we will use (AND) operation.
//Mask value need to be ‘0’ for the bit to unset but rest of the bits
//need to ‘1’. The reason for rest of the bits to set as ‘1’ is as
//we are doing (AND), we don’t want to unset other bits which are
//already set.
//
//For e.g.
//To unset 6th bit 0100 1000
// MASK 1011 1111 (AND)
// ——————————-
// 0000 1000 (Result)
int unset_bit(int val, int num, bool bitval)
{
return (val & ~(bitval << num));
}
//Here, val = 0100 1000
// num = 6
// bitval = 0
// (bitval << num) = 0100 0000
// ~(bitval << num) = 1011 1111
//
// 0100 1000
// 1011 1111 (AND)
// —————————-
// 0000 1000 (Result)
One function to set and unset
int change_bit(int val, int num, bool bitval)
{
return (((val & ~(bitval << num)) | (bitval << num));
}
No comments:
Post a Comment