3

I write a simple steganography tool in C with bmp images.

I read the image to memory and the text to hide in char bytes[8] one character at a time.

so eg.

a=0d97
bytes[0] = 0
bytes[1] = 1
bytes[2] = 1
bytes[3] = 0
bytes[4] = 0
bytes[5] = 0
bytes[6] = 0
bytes[7] = 1

Then i 'll go to the first image byte(char *ptr points it every time) to put the bytes[0] to LSB, then the next one etc.

If the *ptr=0xff or 0b11111111 i have to set the last 1 to 0. This can be with

*ptr = *ptr ^ 0x01 ;

but if the *ptr = 0x00 or 0b00000000 the xor doesent work because 0^1=1

I 'm confused how to set the case. I need a operator to make the last bit every time 0 and not touch the others in case LSB is 1 or 0.

4 Answers4

9

Use this pattern to set the least significant bit to the value in bit (0 or 1):

new_value = old_value & 0xFE | bit

The AND will turn off bit zero and the OR will turn it back on if bit is 1.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
8

The AND of 1 and x is x, while the AND of 0 and x is 0. So, AND it with a number with all bits set to 1 except for the least significant bit:

*ptr = *ptr & 0xfe;

To set the bit to 1, note that the OR of 0 and x is x, while the OR of 1 and x is 1:

*ptr = *ptr | 0x01;
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1

You can use | operator to set the bit. Check out the related post to see more details: How do you set, clear, and toggle a single bit?.

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
0

The most simple answer is. If u want to set the lsb as zero in a number. If the number is odd, Num=num-1. If it is even,it already has zero at lsb.