Questions on bitwise operations: how to calculate bit mask and bit shift based on these 3 fixed values?

user4999

New member
Joined
Jul 28, 2021
Messages
3
Programming Experience
Beginner
I am supposed to determine the bit mask and shift values based on the following 3 values: the start byte, the start bit, and the number of bits.

I will receive some byte array for my data of x many bytes. Let's say some random chunk of data within this large byte array represents whether a light switch is on or off. So I will focus on this single byte/8-bits of data for this example. 3 bits out of these 8 bits are used to determine the on/off state of the light switch.

So, all I know about this chunk of data are these 3 fixed values: the starting byte position within this byte array, the starting bit position within this specific byte itself, and the number of bits allocated to represent the on/off state of this light switch.

So in this particular example, the byte position is byte 15 (within this large byte array of x many bytes). The starting bit position is 0. The number of bits is 3 (since 3 bits are allocated within this byte to represent on/off state).

My question is: how do I calculate the bit mask and bit shift value based on these 3 fixed values? I know the calculation might involve base 2 or raising to the power of 2, but unsure.

Thanks in advance
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
You just need to sit down and think about this problem. It's actually pretty simple, and this is not a C# specific question, but rather a general binary operations question. What have you tried so far? You are correct that the calculation involves raising to the power of 2, but that's for doing things the quick and easy way using math. You first need think about things disregarding the math, and just think in terms of bit operations.

One thing that was not mentioned in your problem statement is how is a bit position defined? Is bit position 0 considered to be the low bit, or the high bit? If it's considered to be the low bit, then why is the start byte computed from the start of the byte array instead of from the end of the array? I know funny little contradictions in how people go from left to right with bytes, and then decide to go from right to left for bits. You'll have to determine if such a contradiction exists in the problem statement.
 

user4999

New member
Joined
Jul 28, 2021
Messages
3
Programming Experience
Beginner
You just need to sit down and think about this problem. It's actually pretty simple, and this is not a C# specific question, but rather a general binary operations question. What have you tried so far? You are correct that the calculation involves raising to the power of 2, but that's for doing things the quick and easy way using math. You first need think about things disregarding the math, and just think in terms of bit operations.

One thing that was not mentioned in your problem statement is how is a bit position defined? Is bit position 0 considered to be the low bit, or the high bit? If it's considered to be the low bit, then why is the start byte computed from the start of the byte array instead of from the end of the array? I know funny little contradictions in how people go from left to right with bytes, and then decide to go from right to left for bits. You'll have to determine if such a contradiction exists in the problem statement.
Thank you for replying. Just to clarify, bit 0 is the lowest bit. In addition, I only need to figure out the mathematical calculation/formula for this specific problem.

Also, can you further elaborate what you mean by "thinking in terms of bit operations"?
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
only need to figure out the mathematical calculation/formula for this specific problem.
If that's true, then this is not even a C# question.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
can you further elaborate what you mean by "thinking in terms of bit operations"?
Just do bit shifts, ANDs, ORs, and NOTs.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
Bitwise operations are really just Boolean operations on individual bits. Numbers in binary are a series of 1s and 0s and a bitwise operation on two numbers is basically just a series of Boolean operations on corresponding bits, where 1 is treated as True and 0 is treated as False. To visualise what's going on in a bitwise operation, just write out the two numbers in binary (Windows Calculator can convert the numbers for you) and then apply Boolean logic to each column, e.g.
C#:
1101 0001 1010 1100
0000 0000 0010 0000
------------------- AND
0000 0000 0010 0000
The result will have a 1 (True) in a position if and only if both operands also have a 1 (True) in that position. That's how logical AND works. You can do similar things for OR, XOR and AND NOT.
 
Top Bottom