0

I have this audio file which I transformed it's audio values to binary numbers so that I hide them in image pixels.

what I used to convert the audio values was bitset<16> since the audio values are stored in short int variables.

the conversion to binary works well even if its a negative number but the conversion from the negative binary value to decimal is not working so does any one know a suitable way to convert negative binary numbers to decimal.

bitset <16>a = -8;
cout<<a<<endl;
output = 1111 1111 1111 1000

bitset <16>b = 8;
cout<<b<<endl;
output = 0000 0000 0000 1000
// but if i tried to convert the binary that i got earlier from the -8
bitset <16> c = 1111111111111000;
cout<< c.to_ulong()<<endl;
output = 29016
// how can i get the output of -8 from the c?  
Allen
  • 3
  • 6
  • Pop quiz: what number is 1111111111111000? Why do you think it should be -8? – Sam Varshavchik Jul 15 '17 at 23:45
  • the only reason I think so is because this is the output that I got from cout< – Allen Jul 15 '17 at 23:50
  • If you write: `int n=1111111111111000`, do you really think you'll wind up with -8 in the variable? Of course not. – Sam Varshavchik Jul 15 '17 at 23:52
  • Is this `1000` an eight or a thousand? You cannot answer. The compiler reads by the syntax rules and decided that it is a thousand. Now compare that to `c = 1111111111111000`. What do you expect the compiler to do? – Yunnosch Jul 15 '17 at 23:56
  • hmm I read the explanation again after your comment and I got it so to represent -8 I go through these steps if am using 16 bit then 8 is 0000 0000 0000 1000 to make it negative I need to inverse first which is this 1111 1111 1111 0111 then add a complement i don't know how to do the last part – Allen Jul 16 '17 at 00:00
  • so how do I get the value of the decimal number @SamVarshavchik – Allen Jul 16 '17 at 00:07
  • When you write `int n=10;`, do you expect to wind up with 10, or 2, in the variable `n`. Of course it's 10. When you write a number in C++ code, it is interpreted as an ordinary base 10 number, always was, always will be. Doesn't matter how big or small it is. End of story. – Sam Varshavchik Jul 16 '17 at 00:10
  • thank you for the explanation and sorry for being a bit slow but please bear with me for a while longer. just now you said that when I write a number um I had a counter to count the number of bits in the audio file that counter when I print the value it was 147456 when I use bitset <16> to print it I get the binary representation and when I use the to_ulong() I get 147456 again. so what you mean by write is if I wrote the number manually by myself? and if so how do I mimic this for negative numbers?or is it already mimicking it and the only problem was that I tried to test this manually?@SamV – Allen Jul 16 '17 at 00:23

0 Answers0