0
How can we access 18 bit integer in a variable?
what data types we can use?
12 Respuestas
+ 1
in this case, try to use int32_t or uint32_t (for unsigned variable)
this is another way to explicitly set size and sign of an integer var
+ 1
if its not declared in this scope make sure it included in the function or it will not recognise it
+ 1
in seperate functions where variables are created upon entering and destroyed upon exiting they fail to see variables outside of there function
eg
int a = 10;
for (x = 0; × < 20 ; x++) {
int a++;
}
may result in an outside of scope error depending on your ide.
+ 1
You don't give a lot of info, but if I understand correctly the easiest is to use a 32bit variable and just mask the higher bits.
On most 32bit systems an int is 32bits as well. (If you have doubts about the size of an int on your platform you can check with sizeof(int). This will give you the size in bytes.)
There are no 18 bit data types in C/C++, so you are going to have to make do with the next size up, which is 32bits.
You do not specify if this must be signed or unsigned, so I am going to assume unsigned (since a signed 18-bit version is going to be much more complicated. If I have space left at the end I will explain).
For unsigned 18bits you use a 32bit int and then just simply mask the higher 14bits of the 32bits. It does not matter if the 32bit int is signed or unsigned since even a signed int has a 31bits positive range - more than enough for 18bits.
So lets use:
int var18x; //Our 32bit var masquerading as an 18bits unsigned var
Lets consider assigning to and assigning from:
//For assigning from our 18bit var
int y = var18x & 0x3FFFF;
The reason we use 0x3FFFF is that, in binary, this is:
Binary: 00000000 00000011 11111111 11111111
Hex: 00 03 FF FF
You can see the high order 14bits are all 0. Bit-ANDing the var18x with this 0x3FFFF mask will set the high 14bits of the result to 0 as well (even if var18x was larger than 0x3FFFF), effectively emulating a 18 bit unsigned var.
//For assigning to our 18bit var:
var18x = y;
var18x &= 0x3FFFF; //This extra step is required to get rid of 'overflow', in exactly the same way as described above.
Of course, for values lower than 0x3FFFF an 18bits unsigned int and a 32bits unsigned int will act exactly the same, as the masking will have no effect. So all we are really doing is limit the range of the var from 0 to 0x3FFFF.
Just note that the 'overflow' flag on the CPU will not be set if our value > 0x3FFFF, but < 0x7FFFFFFF.
0
you can't get exactly 18bits
memory amount used for a variable is divided by 8 bits (1 byte) and commonly multiplies by 2
so, if int (16 bits = 2 bytes) is small for you, you can use long type (32 bits = 4 bytes). that is often more than enough for any common purpose.
0
no,long long int is taking only 16 bit
0
no,long long int is taking only 16 bit
0
thanks
0
int32_t is not working.
it is saying that not declare in this scop.
.
0
give example
0
where are you writing your code and what compiler do you use? I tried to create different int vars here on playground, and everything is OK.
also, I checked the size of vars, and both int and long are 4-byte (32 bits), so as int32_t
0
have you tried to run your code in something more modern and common, like code::blocks IDE?