Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to add a single bit in the nibble as i want to fragment 8 bits data into 2 4 bit nibble's by adding an extra bit to each of them and do on the reception side will receive data in the form of 5 bit, after that will remove the extra bit and Assemble 2 nibble's
Posted
Comments
Richard MacCutchan 17-Nov-14 4:06am    
What exactly are you trying to achieve?

1 solution

First, create a number with N-th bit set and all other bits clear. This value is 1 << N.
On next step, take your original value and calculate the bitwise OR with the value from the previous step.
That is:
C++
int N = //... bit number
int value = //... can be any integer type, signed or unsigned
value |= 1 << N; // same as:
value = value | (1 << N)


To clear a bit, use bitwise AND with the complement:
C++
value &= ~(1 << N);


That's all. Learn the bitwise operators: http://www.learncpp.com/cpp-tutorial/38-bitwise-operators[^].

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900