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.

  1. Write Great Code Volume 1: Understanding the Machine
  2. Write Great Code Volume 2: Thinking Low-Level, Writing High-Level
  3. Algorithms + Data Structures = Programs