Bit Manipulation And Bitwise Flashcards

1
Q

Bit manipulation

A

Modifying individual bits within an object, used for encription and compression algorithm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Bit flags

A

Bits when individual bits of an object are used as boolean values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A flag

A

In computing, a value that acts as a signal for some function or process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Defining a set of bit flags

A

Using unsigned integer of the apropriatesize depending on how many flags we have or std::bitset

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Bit numbering

A

In a sequence we number the bits from right to left starting with 0, each number denotes bit position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Functions that are useful for bit manipulation

A

Std::bitset provides 4

  • test()
  • set()
  • reset()
  • flip()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Test()

A

Allows us to qmuery whether a bit is a 0 or 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Set()

A

Allows us to turn a bit on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Reset()

A

Allows us to turn a bit off

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Flip()

A

Allows us to flip a bit value from 0 to 1 and vice versa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Bitwise operators

A

Bot manipulation operators

<>,~,&,| and ^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Bitwise left shift

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Bitwise right shift&raquo_space;

A

Shifts bits to the right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Operators &laquo_space;and&raquo_space;

A

Are used for output and input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Overloaded

A

Provided an alternative definition for operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Bitwise NOT ~

A

Flips each bit from 0 to 1 and vice versa, the result is dependent on the size of the data type

17
Q

Bitwise OR |

A

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

18
Q

Bitwise AND &

A

Evaluates to true 1 if both bits in the column are 1

19
Q

Bitwise XOR ^

A

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

20
Q

Bitwise assignment operators

A

«=,&raquo_space;=, |=, &=, ^=

Instead x=x»1, you can write x»=1

21
Q

Bit mask

A

Predefined set of bits that is used to select wich specific bits will be modified by subsequent operator

22
Q

Simplest set of bit mask

A

To define one bit mask for each bit position, defined as symbolic constants to be reused

23
Q

Testing a bit

A

Use a bit mask in conjunction with a bit flag variable

24
Q

Setting a bit

A

Use a bit mask in conjunction with bitwise or equals =

25
Q

Resetting a bit

A

We use bitwise and bitwise not

26
Q

Flipping a bit

A

We use bitwise XOR

27
Q

Bit masks and std::bitset

A

The functions alloes us to modify individual bits, the operators to modify multiple bits at once

28
Q

Use for bit flags

A
  • 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
29
Q

Binary numbers

A

Work like decimals, because there are only 2 binary digits, the value of each digit increases by factor of 2

30
Q

Converting binary to decimal

A

8 bit number 0101 1110 is (0×128)+(1×64)+(0×32)….=94

31
Q

Converting decimal to binary

A

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…

32
Q

Adding in binary

A

0110 + 0111= 0+1=1, 1+1=0 carried 1, 1+1=0+1carreid =1, 0+0=0

33
Q

Two’s complement

A

Method for storing signed integers

34
Q

Positive signed numbers

A

Represented in binary just like positive unsigned numbers, with sign bit of 0

35
Q

Negative signed numbers

A

Represented in binary as the bitwise inverse of the positive number +1

36
Q

Converting integers to binary two’s complement

A

5 in binary is 0000 0101, inverted is 1111 1010, we add 1=1111 1011

37
Q

Why we add 1 to convert integer to binary two’s complement

A

If we only inverse it 0 will have 2 representations 0000 0000 and 1111 1111 so we add 1 to overflow

38
Q

Converting binary two’s complement to integers

A

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

39
Q

Why tipes matter

A

If it is unsigned it can store numbers of values, but if it is signed it can only store half of that number.