Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 561 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing a Bit in C Programing

#1
I am coding in C programming.
Let's say I have a character:
char letter=0x0000;
So the binary data for the letter is now "00000000"
let say I want change the binary data to "10000000" and later to change it "10010000", is there a bitwise operator or method that would allow me to change a "0" to a "1" or "1" to a "0" at a specific position. Also is this possible?
Reply

#2
You can use bitwise AND (&) and OR (|) operator.
For example:

01001000 |
10111000 =
11111000

This is done following:
72 | 184 = 248

(72 = 64+8)

For details see following tutorial:

[To see links please register here]

Reply

#3
Yes it is very much possible. Just use bitwise exclusive OR or simply XOR operator on the number with 2 to the power of n where as n is the digit you want to change. ^ is the XOR operator in C.

000000 (decimal 0)
^ 100000 (decimal 32 = 2 power 5 = 1 << 5)
= 100000

1010 (decimal 10)
XOR 0010 (decimal 2 = 2 power 1 = 1 << 1)
= 1000

You can calculate 2 to the power of n by simply shifting bits in 1 by n bits. So 2 to the power of 4 can be obtained by shifting bits in 1 by 4 places.

inputNum ^ (1 << n) will give what you needed if toggling is all you need.

Bitwise XOR "^"
bit a bit b a ^ b (a XOR b)
0 0 0
0 1 1
1 0 1
1 1 0

However remember that doing XOR on a bit that already has 1 will convert it to zero. because 1 ^ 1 = 0;

If you just want to convert 0 to 1 and keep 1 if it is already there. You can have to use bitwise Or operator.

Bitwise OR "|"
bit a bit b a | b (a OR b)
0 0 0
0 1 1
1 0 1
1 1 1

Following is an example

11001110
| 10011000
= 11011110

Source:

[To see links please register here]

&

[To see links please register here]

Reply

#4
If you XOR any bit with a 1 bit, it toggles its value:

0 ^ 1 = 1
1 ^ 1 = 0

Similarly, if you XOR any bit with 0, it keeps the same value:

0 ^ 0 = 0
1 ^ 0 = 1

Therefore, you can flip the nth bit of a number by XORing it with a number that has zero bits everywhere except in bit n:

val ^= (1 << n);

Hope this helps!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through