How can we access 18 bit integer in a variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can we access 18 bit integer in a variable?

what data types we can use?

14th Dec 2016, 10:34 PM
RITESH KUMAR
RITESH KUMAR - avatar
12 Answers
+ 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
14th Dec 2016, 10:51 PM
Demeth
Demeth - avatar
+ 1
if its not declared in this scope make sure it included in the function or it will not recognise it
14th Dec 2016, 11:07 PM
Jason Hoffman
Jason Hoffman - avatar
+ 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.
14th Dec 2016, 11:19 PM
Jason Hoffman
Jason Hoffman - avatar
+ 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.
15th Dec 2016, 12:48 AM
Ettienne Gilbert
Ettienne Gilbert - avatar
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.
14th Dec 2016, 10:45 PM
Demeth
Demeth - avatar
0
no,long long int is taking only 16 bit
14th Dec 2016, 10:49 PM
RITESH KUMAR
RITESH KUMAR - avatar
0
no,long long int is taking only 16 bit
14th Dec 2016, 10:49 PM
RITESH KUMAR
RITESH KUMAR - avatar
0
thanks
14th Dec 2016, 10:53 PM
RITESH KUMAR
RITESH KUMAR - avatar
0
int32_t is not working. it is saying that not declare in this scop. .
14th Dec 2016, 11:04 PM
RITESH KUMAR
RITESH KUMAR - avatar
0
give example
14th Dec 2016, 11:09 PM
RITESH KUMAR
RITESH KUMAR - avatar
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
14th Dec 2016, 11:13 PM
Demeth
Demeth - avatar
0
have you tried to run your code in something more modern and common, like code::blocks IDE?
14th Dec 2016, 11:20 PM
Demeth
Demeth - avatar