Bit Manipulation And Bitwise Flashcards
Bit manipulation
Modifying individual bits within an object, used for encription and compression algorithm
Bit flags
Bits when individual bits of an object are used as boolean values
A flag
In computing, a value that acts as a signal for some function or process
Defining a set of bit flags
Using unsigned integer of the apropriatesize depending on how many flags we have or std::bitset
Bit numbering
In a sequence we number the bits from right to left starting with 0, each number denotes bit position
Functions that are useful for bit manipulation
Std::bitset provides 4
- test()
- set()
- reset()
- flip()
Test()
Allows us to qmuery whether a bit is a 0 or 1
Set()
Allows us to turn a bit on
Reset()
Allows us to turn a bit off
Flip()
Allows us to flip a bit value from 0 to 1 and vice versa
Bitwise operators
Bot manipulation operators
<>,~,&,| and ^
Bitwise left shift
Shifts bits to the lef, lefto operand is the expression to shift the bits of, right operand is integer number of bits to shift left by x«1, new bits recieve value 0
Bitwise right shift»_space;
Shifts bits to the right
Operators «_space;and»_space;
Are used for output and input
Overloaded
Provided an alternative definition for operator
Bitwise NOT ~
Flips each bit from 0 to 1 and vice versa, the result is dependent on the size of the data type
Bitwise OR |
Works lige logical or, but instead of aapplying the or to the operand it applise it to each bit, evaluates 1 if eather left, right or both bits are 1, 0 otherwise
Bitwise AND &
Evaluates to true 1 if both bits in the column are 1
Bitwise XOR ^
Exclusive, or evaluates true if one and only one operand is true, if neither or both are true it evaluates to 0, in compound expressions if there is an odd number of 1 bits the result is true, even number is false
Bitwise assignment operators
«=,»_space;=, |=, &=, ^=
Instead x=x»1, you can write x»=1
Bit mask
Predefined set of bits that is used to select wich specific bits will be modified by subsequent operator
Simplest set of bit mask
To define one bit mask for each bit position, defined as symbolic constants to be reused
Testing a bit
Use a bit mask in conjunction with a bit flag variable
Setting a bit
Use a bit mask in conjunction with bitwise or equals =
Resetting a bit
We use bitwise and bitwise not
Flipping a bit
We use bitwise XOR
Bit masks and std::bitset
The functions alloes us to modify individual bits, the operators to modify multiple bits at once
Use for bit flags
- when you have many identical flag variables
- when you have a function that can take any combinations of 32 different options
- to pass in only the options you wanted
Binary numbers
Work like decimals, because there are only 2 binary digits, the value of each digit increases by factor of 2
Converting binary to decimal
8 bit number 0101 1110 is (0×128)+(1×64)+(0×32)….=94
Converting decimal to binary
2 methods
* dividing by 2 and writing down the remainders, the binary number is constructed from the remainders from the bottom up
* 148 largest power of 2 thats smaller than 148=128
148>=128 yes, 128 bit is 1, 148-128=20
20>=64 no, 64 bit is 0…
Adding in binary
0110 + 0111= 0+1=1, 1+1=0 carried 1, 1+1=0+1carreid =1, 0+0=0
Two’s complement
Method for storing signed integers
Positive signed numbers
Represented in binary just like positive unsigned numbers, with sign bit of 0
Negative signed numbers
Represented in binary as the bitwise inverse of the positive number +1
Converting integers to binary two’s complement
5 in binary is 0000 0101, inverted is 1111 1010, we add 1=1111 1011
Why we add 1 to convert integer to binary two’s complement
If we only inverse it 0 will have 2 representations 0000 0000 and 1111 1111 so we add 1 to overflow
Converting binary two’s complement to integers
We look at the sign bit, if it is 0 we convert the numbers as unsigned, if it is 1 we invert the bits, add 1, convert to decimal, make the decimal negative
Why tipes matter
If it is unsigned it can store numbers of values, but if it is signed it can only store half of that number.