|
From: Carlos d. M. <cg...@wo...> - 2002-06-08 19:26:55
|
Hi:
I have problem I want solve by bit shifts. Below I add the code (one of
the many versions I tried) so you can see it and know better what I am
searching. The thing I cannot do at any way is the shifting phase
(obviously, if not the subject would have been another).
I want take some bytes and put them in a variable with more than one
byte of width. For example, a double or a long double variable.
So I try unsigned char unschar; I make an struct with eight unschar, I
assign the first eight bits to the first and shift it 7*8 places when
assigning to double, the next 8 bits to the second, shifting eight bits
less, and so on. But everyone except the last "warning shift is >= than
the width of the type". The same if I try with unsigned int.
If I try changing unchars by doubles, so they have the size I need for
the shifts and that warning cannot appear, but then "error using double
with int in binary >> operator". If I use with variables two doubles it
says the same error "using double with double in >> binary operator".
If I change to a struct with void varname:64; error because void is not
"forgotten" (I don't remember the exact word), or there is no cast to
int (in which case I have again the problem of shifting more than the
width of the type, I suppose, in the trial of this it still says error
because the buffer is void type or if I try int tal:64, then it exceeds
its type).
Finally, with temp as *unschar, the void_type{ int pues:64;}vacio;, I
read with temp, assign it with (int) cast to pues and shift pues it
reads it wrong and creates and error, windows has to close it.
With long double it says no more than 64 width bitfields.
Is there some way to do that and not having to be oneself who reads the
bits?
Here are the sizeofs of my computer:
char: 1
unsigned char: 1
unsigned long int: 4
short int: 2
short: 2
unsigned short int: 2
int: 4
float: 4
double: 8
unsigned char: 1
unsigned int: 4
long int: 4
long double: 12
The program:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <stdio.h>
typedef struct new_doble{
unsigned int dospri;
unsigned int dosult;
}doble;
typedef struct signed_doble{
int sig1;
int sig2;
}siged;
typedef struct novo_dos{
unsigned char uno;
unsigned char dos; //These two are from a trial with double, trying to
make a double with all 1 I get only 1.447e+09
unsigned char tres;
unsigned char cuatro;
unsigned char cinco;
unsigned char seis;
unsigned char siete;
unsigned char ocho;
}dos;
typedef struct complet{
double every;
};
typedef struct char_type{
unsigned char hype:64;
}extra;
typedef struct void_type{
int pues:64;//This was void in the previous trials
}vacio;
int main()
{
doble pares;
siged coplus;
dos octos;
complet all;
int pri, ult;
long double hala;
double tal;
double test = 747665.4576;
unsigned char mascara = 255;
short int mask = 65535;
int zorro = mask + (mask << 15);
double temp;
unsigned char *pol;
vacio nada;
extra largo;
fstream outFile;
fstream inFile;
FILE *fuera;
FILE *dentro;
cout << "zorro " << zorro << '\t' << mask << endl;
outFile.open("shifting", ios::out);
if(!outFile){
cout << "shifting no abre" << endl;
exit(1);
}
outFile << test;
outFile.close();
if((fuera = fopen("shifting", "r")) == NULL){
cout << "shiftin2 no abre" << endl;
exit(1);
}
fseek(fuera, 0, SEEK_END);
int size;
size = ftell(fuera);
rewind(fuera);
int tanto = 55;
while(!feof(fuera) && tanto >= 0){
fread(pol, 1, 8, fuera);
nada.pues=(int)pol;
tal = nada.pues << tanto;
tanto-=8;
}
fclose(fuera);
cout << test << '\n' << tal;
system("PAUSE");
return 0;
}
|