C++ last bit of an image


 
Thread Tools Search this Thread
Top Forums Programming C++ last bit of an image
# 1  
Old 04-02-2011
C++ last bit of an image

Hi guys, I've been trying to find out what's wrong with the code, g++ doesn't prompt any error but it doesn't do what I want it to do, and after looking at it over and over I can't find anything wrong with it. The code its quite simple:

I need to save the last bit of every position on the vector and to form a word, for example:
buffer[0] has 00110011
buffer[1] has 11000010

I need to get the last bit and form a letter every 8 bits to decode the sentence.


buffer is a dinamic vector that has the image info loaded on it, cadena is where I want to store the "decoded " letters

Code:
 void revelarPGM (char* &cadena,unsigned char buffer[]) { 
 int binario=0;			
 int j=0;	
 cadena=new char[50000];
 for (int i=0; i<262144; i++) {
     if((buffer[i]&1)==0){ // HERE i use the AND operator to get the last bit, if it is
           binario=(binario<<1);  // A 0, i just shift once
     }else{   
	   binario=(binario<<1);  //IF it is a 1, i shift and add a 1 with the OR
  	   binario=(binario|1);    
     }
   if (i%7==0) {               // WHEN i get 8 bits i store the letter in cadena
          cadena[j]=binario;
          j++;
	  cout << binario << endl;
          binario=0;
  }
 }
 for (int i=0; i<300; i++)
cout << cadena[i];  // cout to see the words i stored.
delete [] cadena;
}

I hope I made myself clear. The problem is I am getting rubbish. But everytime i look at the code, it seems alright to me.

edit: tried to edit the title since i forgot to add it was C++ but it wont allow me now.

Last edited by lamachejo; 04-02-2011 at 03:52 PM..
# 2  
Old 04-03-2011
The "(i%7==0)" is incorrect. It only works the first time i hits 7.

It should be
Code:
( ( ( i + 1 ) % 8 ) == 0 )

And not the parenthesis. I'm not sure about operator precedence between "+", "==", and "%".

Think of it as, "If the NEXT iteration is divisible by 8, reset to the next byte."
This User Gave Thanks to achenle For This Post:
# 3  
Old 04-03-2011
Thanks a bunch, that was the problem. Now it works flawlessly.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

3. Red Hat

boot the 32 bit kernel on a 64 bit PPC Linux machine?

Hi all, I'm looking to cover a corner case for an upcoming test cycle. Is there a way to boot a RedHat Advanced Server 4 (update 3) installed on a Power PC machine to use a 32 bit kernel? This would be similar to what is done here -> https://www.unix.com/aix/26204-aix-platform.html I've done... (0 Replies)
Discussion started by: philrau
0 Replies

4. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies
Login or Register to Ask a Question