c - bit-shifting by an integer value -
this code cache simulator project - trying extract bits memory address. when attempt use int variables bit shifting, end incorrect result, when use numbers directly, result correct. i've been looking on answer this, can't find one. issue here?
#include <stdio.h> void main(){ unsigned long long int mem_addr = 0x7fff5a8487c0; int byte_offset_bits = 2; int block_offset_bits = 5; int index_bits = 8; int tag_bits = 33; unsigned long long int tag1 = (mem_addr&(((1<<33)-1)<<(2+5+8)))>>(2+5+8); unsigned long long int tag2 = (mem_addr&(((1<<tag_bits)-1)<<(byte_offset_bits + block_offset_bits + index_bits)))>>(byte_offset_bits + block_offset_bits + index_bits); printf("%s %llx\n", "tag 1:", tag1); printf("%s %llx\n", "tag 2:", tag2); }
the output is:
tag 1: fffeb509 tag 2: 1
i getting warning line computes tag1 correctly, doesn't make sense me, since tag1 64-bit unsigned long long int, , shifting 48 bits:
warning: left shift count >= width of type [enabled default]
thanks in advance help.
all integer literal values int
type, unless specify prefix such e.g. 1ull
.
that means 1<<33
shifts 32-bit signed value 33 steps. need 1ull << 33
.
Comments
Post a Comment