2

It's pretty simple. Let's say I have:

char x = -1;

Then in memory, I have (most likely?)

 11111111
(01234567)

So my question is, if I write &x is the address I get back the address of bit 0 or bit 7 and is this the most or least significant bit?

What about in the case of a 32bit integer?

jrbalsano
  • 834
  • 7
  • 18
  • 5
    On most architectures, bits aren't addressable, so the address designates the full byte. Re 32-bit integers, that depends on the endianness of the platform. – Daniel Fischer Mar 25 '13 at 17:07
  • Yes, how are you proposing to get the address of a *bit* exactly? The premise is flawed because you cannot. – Ed S. Mar 25 '13 at 17:09
  • Sorry about that. I asked a poor question being rushed. The answers that I got helped me out though. I was looking to know the case of a 32bit integer with 4 bytes, which byte is pointed to by the address. – jrbalsano Mar 25 '13 at 20:04

6 Answers6

5

the memory address are byte-addressed, not bit addressed. Which means you will - depending upon the type of architecture(big endian or little endian) you will get the most significant byte or least significant byte not specific bit

Thanks to WhozCraig, yes - since your variable is a char it is the address of the char or the byte itself.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
3

Bits are not individually addressable. The smallest addressable unit of memory is the byte. So in the case of a char, when you take its address you get a pointer to that byte. Not any particular bit within that byte, the whole byte. The only way to access individual bits within that byte is via bitwise operations like masking and shifting.

For a 32-bit integer you can address the 4 bytes individually. In that case whether its address is the address of the least significant or the most significant byte depends on the architecture's endianness. On a big endian system the address will be the address of the most significant byte. On a little endian system it will be the least significant byte.

uint32_t n = 0x11223344;
uint8_t *p = (uint8_t *) &n;

if (*p == 0x11) {
    // big endian
}
else if (*p == 0x44) {
    // little endian
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2

In C, the memory is byte addressable, also interpretation of char is byte. A char is consist of one byte and for example int is consist of four bytes so char x; then &x; is address of char not bit/byte. if youe take int i then &i is address of int object. we are refreshing variable/ complete object.

In memory address scheme is like, each bytes are memorized(address stored).

  0     1   2    3   4   5    6     7    8   9    10   11  12  13   14   15 
+----+----+----+---+---+----+----+----+----+----+----+---+---+----+----+----+
| 1  |  2 | 3  | 4 | 5 |  6 |  7 | 8  | 0  |  1 |  1 | 2 | 3 | 4  |  5 | 6  |  
+----+----+----+---+---+----+----+----+----+----+----+---+---+----+----+----+
             ^                          ^
             |                          |
+----+----+  |                          |
|   add1  |--|                          |
+----+----+                             |
|   add2  |-----------------------------|
+----+----+ 

In this figure addr1 represents bit 0-7 in case of char and addr2 represents bit 8-15. I mean to say ofcouser bit can be numbered(0-15), but efficient is to access one byte at a time. and even if you need one bit information its good to read complete byte.

Have you read this question: What exactly is a C pointer if not a memory address?. If your asking about C: A pointer value can be some kind of ID or handle or a combination of several IDs

So &x is just an reference in C. It references one byte for char and four byte for int

Additionally,

Although memory is byte addressable but fortunately, we have access at bit level thought bitwise operator and bitwise structure.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • yes, I remember that linked question of "what exactly is a C pointer.. " pretty well. It enlightened me and I am still awestruck at the deep understanding of AlexeyFrunze(a Microsoftie btw). Excellent question as well as the most insightful answer by Alexey there. – Aniket Inge Mar 25 '13 at 18:11
  • @Aniket Yes, the question was interesting and AlexeyFrunze really gives good answers. BTW which clg you are from? In your profile you have not mentioned. – Grijesh Chauhan Mar 25 '13 at 18:20
  • I am done with colleges. I studied electrical engineering in BMSCE bangalore. @GrijeshChauhan – Aniket Inge Mar 25 '13 at 18:23
  • @Aniket but you have done good stuff in computer science. an better than me I am from CS and from [MNIT](http://www.mnit.ac.in/new/) – Grijesh Chauhan Mar 25 '13 at 18:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26889/discussion-between-aniket-and-grijesh-chauhan) – Aniket Inge Mar 25 '13 at 18:46
1

Addresses refer to bytes in memory -- bits don't have their own individual addresses.

That said, lets assume that the value is larger than a single byte:

long x = -1;

In that case, the value takes up 32 bits, or 4 bytes. Whether those bits are stored with the most significant byte first or the least significant byte first depends on the hardware involved. More specifically, it depends on whether the hardware is little endian or big endian. Either way, the address of x in the example above will be the address of the first (lowest) byte in memory, so if you have:

char p[4] = (char*)&x;

then p[0] will be the first byte, p[1] the second byte, and so on. Endianness just tells you whether the first byte is the most significant or the least.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

Addresses refer to entire bytes, so it doesn't make sense to ask about the address of a bit. Most modern computers are little-endian (see this question), so you should get the address of the least significant byte.

Community
  • 1
  • 1
raptortech97
  • 507
  • 3
  • 14
0

As pointed out by most guys, memory is byte-addressable and bits don't have individual addresses. Still if you want to access the bits(read/write into them), you can use the bit-shift operations with bitwise logical ones.

CuriousSid
  • 534
  • 3
  • 11
  • 25