OK so i'm just gonna reiterate what my question title said. unsigned short short int is 1 byte with range of 0-255 right? the reason i ask is because when i Google "c++ byte type" i get no hits that specify a standard byte typdef, my compiler seems to be aware of two "byte" and "BYTE". i am uneasy about using them because i haven't found any documentation on them. how ever it seems to me that short short int is a byte and therefor i could make my own typedef for a byte right.
13 Replies - 3830 Views - Last Post: 19 June 2010 - 09:01 PM
#1
unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 03:16 PM
Replies To: unsigned short short int is 1 byte with range of 0-255 right?
#2
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 03:34 PM
ishkabible, on 19 June 2010 - 02:16 PM, said:
OK so i'm just gonna reiterate what my question title said. unsigned short short int is 1 byte with range of 0-255 right?
most likely not. On a standard 32bit average joe system, a char and bool are one byte, short is 2 bytes, int is 4 bytes, and double 8. However, there aren't any set guarantees about any of this, and many compilers are 'ad-hoc' about it.
If you really need to know the size of different types, use the sizeof operator to check
#include <iostream>
using namespace std;
int main() {
cout << "A char is " << sizeof(char) << " byte.\n";
cout << "An unsigned short is " << sizeof(unsigned short) << " byte.\n";
return 0;
}
Just insert the proper types
As for the range, just use the formula of size = 2^sizeof(type). If the type is signed, then this will need to be cut in half.
Hope I helped
#3
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 03:40 PM
My compiler complains about unsigned short short int i;.
So I would say short short is not correct.
climits (limits.h)
So I would say short short is not correct.
climits (limits.h)
#4
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 03:49 PM
wierd mine doesn't complain at all, i think unsigned char will do the trick now that i look in to it a bit more. or is char by default unsigned, it would make sense that it would be unsigned by default.
#5
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 04:13 PM
Be careful to distinguish between C++ bytes and system bytes. C++ thinks about an abstract system. For example, a C++ char is always one byte, by definition. Whether on your computer this is 8 bits and 1 byte, or 64 bits and one byte, or 64 bits and 8 bytes, or 32 bits or 128 bits, is irrelevant.
So be sure to distinguish between C++ and your actual system...
So be sure to distinguish between C++ and your actual system...
#6
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 04:19 PM
Do not rely on your compiler's type sizes being the same as anyone else's.
If you do, and you ever get a new computer/compiler, or someone tries to port your app to another system, if will crash and burn... There's usually a way of doing things where it's just not necessary to know.
If you do, and you ever get a new computer/compiler, or someone tries to port your app to another system, if will crash and burn... There's usually a way of doing things where it's just not necessary to know.
#7
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 04:56 PM
well maybe it would be in my interest to say what i was going to use this for.i was going to make my own class that would have the ability to stack an undefined number of bytes then using different functions read them as either integers, floats, or strings(the stack it's self as a integer float or string). i thought about using operator overloading for it but see now that there are too limited a number of operators that make sense. the idea is for a way to store different types of data from the same variable (or at least make it seem like your are).
edit:i was unclear
edit:i was unclear
This post has been edited by ishkabible: 19 June 2010 - 04:58 PM
#8
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 05:03 PM
Quote
i was going to make my own class that would have the ability to stack an undefined number of bytes then using different functions read them as either integers, floats, or strings
#9
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 05:36 PM
its for a simple scripting language, i want a single var that can hold almost any kind of data, the idea is that the var represents a sort of sudo memory. i just don't know the best what to do it. to be more clear the memory would be a hash_table that stored my class type and indexed by name(string).
#10
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 06:52 PM
That's what void * is for. In addition to the normal data types, there are also pointers. Pointers are simply another variable that store the location of some object in memory. Pointers also have types, so the way to hold anything is to use a void pointer.
#11
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 06:54 PM
A union might be another option for you.
#12
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 07:22 PM
ok I'm not sure how to use pointers very well as i have always avoided them due to the fact that i find them very abstract, i'm also not sure how to use a union. that's why i'm posting my code so you can better see my problem(this is no where near done).
so if you look at my SetKeywordsAndValue and SetOpsAndValue function's you will see that the whole of both functions do not work if typedef hash_table uses void* as the stored data type. how would i go about using a void* as the stored data type to store int, and strings both.
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map.hpp>
#include<boost/tokenizer.hpp>
#include <boost/any.hpp>
using namespace std;
using namespace boost;
typedef boost::unordered_map<std::string, void* > hash_map;
typedef boost::tokenizer<boost::char_separator<char> > Tok;
template <class StackType> class Stack {
public:
std::vector<StackType> stack;
StackType push(StackType val);
StackType pop();
StackType read(int index);
};
template <class StackType> StackType Stack<StackType>::push(StackType val) {
stack.insert(stack.end(),val);
}
template <class StackType> StackType Stack<StackType>::pop() {
const StackType x = stack.at(stack.size()-1);
stack.pop_back();
return x;
}
template <class StackType> StackType Stack<StackType>::read(int index) {
return stack.at(index);
}
typedef Stack<int> intStack;
typedef Stack< std::string > StrStack;
hash_map Keywords;
hash_map Ops;
hash_map Varibles;
StrStack TokenStack;
intStack CompileStack;
intStack RuntimeStack;
void SetKeyWordsAndValue() {
Keywords["push"] = -1;
Keywords["pop"] = -2;
Keywords["var"] = -3;
Keywords["if"] = -4;
Keywords["do"] = -5;
Keywords["end"] = -6;
Keywords["jmp"] = -7;
Keywords["lbl"] = -8;
Keywords["and"] = -9;
Keywords["or"] = -10;
Keywords["get"] = -11;
Keywords["write"] = -12;
Keywords["not"] = -13;
Keywords["EOP"] = -14;
}
void SetOpsAndValue() {
Ops["+"] = -15;
Ops["-"] = -16;
Ops["*"] = -17;
Ops["/"] = -18;
Ops["<"] = -19;
Ops[">"] = -20;
Ops["="] = -21;
}
int AscciToInt( int Ascci ) {
int tempI;
tempI = Ascci - 48;
return tempI;
}
int ReadNumber(string num) {
int x,i,y;
x=0;
y=0;
for(i=num.length()-1;i>=0;i--) {
x += AscciToInt(num[i])*pow(10.0,y);
y++;
}
return x;
}
string Load( std::string sFile) {
std::ifstream fp(sFile.c_str());
std::string stri;
while (!fp.eof()) {
int temp = fp.get();
stri.push_back( temp );
}
fp.close();
}
void SetTokens(std::string Script) {
typedef boost::tokenizer<boost::char_separator<char> >
tokenizer;
boost::char_separator<char> sep(" |{}[]$@!'#%^&()_\~`,.?","+-*<>=/;", boost::drop_empty_tokens);
tokenizer tokens(Script, sep);
for (tokenizer::iterator tok_iter = tokens.begin();tok_iter != tokens.end(); ++tok_iter) {
std::cout << "<" << *tok_iter << "> ";
string PUSH(*tok_iter);
TokenStack.push(PUSH);
}
std::cout << "\n";
}
void CompileTokens() {
bool end=1;
int index=0;
index=0;
while(end) {
if(TokenStack.read(index) == "push") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "pop") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "var") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "if") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "do") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "end") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "jmp") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "lbl") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "and") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "or") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "get") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "write") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "not") CompileStack.push(Keywords[TokenStack.read(index)]);
if(TokenStack.read(index) == "EOP") {end = 0; CompileStack.push(Keywords[TokenStack.read(index)]);}
if( Keywords[TokenStack.read(index)] == NULL && Ops[TokenStack.read(index)] == NULL) {
CompileStack.push( ReadNumber(TokenStack.read(index)));
}
if( TokenStack.read(index) == "+") CompileStack.push(Ops[TokenStack.read(index)]);
if( TokenStack.read(index) == "-") CompileStack.push(Ops[TokenStack.read(index)]);
if( TokenStack.read(index) == "/") CompileStack.push(Ops[TokenStack.read(index)]);
if( TokenStack.read(index) == "*") CompileStack.push(Ops[TokenStack.read(index)]);
if( TokenStack.read(index) == "=") CompileStack.push(Ops[TokenStack.read(index)]);
cout<< "<"<<CompileStack.read(CompileStack.stack.size()-1)<<"> ";
index++;
}
}
void Compile(std::string ScriptFile) {
SetTokens(Load(ScriptFile));
CompileTokens();
}
void Run() {
bool end=1;
int index=0;
while(end) {
}
}
int main() {
//SetKeyWordsAndValue();
//SetOpsAndValue();
//SetTokens("push 1000 push 33245 + EOP");
//CompileTokens();
ByteStack ByteStackTest;
ByteStackTest.Setint(1002506);
cout<<ByteStackTest.readint();
return 1;
}
so if you look at my SetKeywordsAndValue and SetOpsAndValue function's you will see that the whole of both functions do not work if typedef hash_table uses void* as the stored data type. how would i go about using a void* as the stored data type to store int, and strings both.
#13
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 08:21 PM
ok i think this will work but i don't know two things, how to print where it's not in hex and if all of them will just equal the same thing because there all pointing to the same varible;
void SetKeyWordsAndValue() {
x=-1;
Keywords["push"] = &x;
x=-2;
Keywords["pop"] = &x;
x=-3;
Keywords["var"] = &x;
x=-4;
Keywords["if"] = &x;
x=-5;
Keywords["do"] = &x;
x=-6;
Keywords["end"] = &x;
x=-7;
Keywords["jmp"] = &x;
x=-8;
Keywords["lbl"] = &x;
x=-9;
Keywords["and"] = &x;
x=-10;
Keywords["or"] = &x;
x=-11;
Keywords["get"] = &x;
x=-12;
Keywords["write"] = &x;
x=-13;
Keywords["not"] = &x;
x=-14;
Keywords["EOP"] = &x;
cout<<Keywords["EOP"];
}
#14
Re: unsigned short short int is 1 byte with range of 0-255 right?
Posted 19 June 2010 - 09:01 PM
OK i finely figured it out
my hash table is a set of void pointers that point to different places in a vector that way no variables get mixed up;
Page 1 of 1
|
|

New Topic/Question
Reply







MultiQuote






|