# A low level question related to computer memory?

why Integer variable takes 4 bytes to store a number where a character takes 1 bytes as I know about computer memory characters are converted into number before they store into memory, So, Is character should not takes 4 bytes to store into memory. How exactly integer, characters and floating data store into memory can anybody explain me?

No, they arenâ€™t converted to numbers. Something of type char isnâ€™t converted to a number, itâ€™s a byte (if the size is actually just 1 byte). A byte is going to represent a range of possible values, whatâ€™s important is how many combinations of bits you can have.

So one byte is 8 bits, so thatâ€™s 2^8 (each bit can be on or off), which is 256 different combinations. So if (if, it isnâ€™t necessarily) a char is 8 bits, a char can represent 256 different characters. Or integers from 0-255. Or whatever.

As a practical example, RGB colour is represented by three 8-bit values (r 0-255, g 0-255, b 0-255). So hexadecimal colour codes (`#ffffff`, `#d0d0d0`) are just three bytes, with each byte representing an unsigned 8-bit number.

A certain size of integer takes 4 bytes, ie a 32-bit, so 4 bytes. So that gives a possible 2^32 number of integers (either signed or unsigned, so i32 or u32).

Another common type of integer is 8 bit, another is 64 bit, etc. Itâ€™s just how many different values you can have. â€śin {this} much memory I can represent {this} many discrete valuesâ€ť.

Floating point is two integers â€“ a number of fixed digits (the significand), and an exponent. Then the computer can calculate what it is by running significand Ă— base^exponent). So again, you have different sizes in terms of bits, and they explain the range of numbers that can be represented.

2 Likes

So, I do some more research and found that the char is also take 4 bytes but the character is limited and almost all characters are represented in 256 value combinations and 1 byte is sufficient to represent 256 valueâ€™s so in terms of performance characters are represented into 1 bytes in terms of ASCII not Unicode but the number are infinite and it is not possible to represent them into 1 bytes so we need more bytes to store them like 32 bitâ€™s combinations store 2^32(approx. 2 billion) and 64 bit combinations store 2^64(approx. 18 quintrillian) numbers, is Iâ€™m right?

Yes, but itâ€™s dependent on the language and compiler. 256 values gives you the ASCII character set, yes. Unicode has around 144000 characters; 256 is not enough. Thatâ€™s where encodings come in â€“ to support the most common, UTF-8, you need up to 4 bytes to cover the entire character set

Yes there are infinite numbers, but

1. computers are finite,
2. itâ€™s unusual to need extremely large numbers and
3. there are [mathematical and/or programmatic] ways of representing very large numbers that mean often donâ€™t need to represent them literally
1 Like

Will you tell me any good resource to study theory of computer science for my collage exam?

your professor may have some suggested books, I would look in that as first thing, anything that could be suggested here may be still about theory of computer but not on the material for your exam

1 Like

Yeah, as above. I donâ€™t have a CS background and Iâ€™ve just picked up what I know from reading a lot of stuff [for example 1, 2, 3], and that stuff will likely not quite apply to your specific course.

I donâ€™t know that what youâ€™re asking about in this thread is really CS (per se) though. How itâ€™s implemented at an algorithmic level is an application of CS, but itâ€™s nuts and bolts engineering, if you see what I mean.