我需要使用结构访问带符号的短型数字的高位,低位和数字位。 对此一无所知,正在浏览互联网,但是关于签名短的信息并不多。
我试图使用某些功能来运行它,但是我的任务是使用位域...
void print(signed short num)
{
for (int i = 0; i < 16; i++)
{
if (num&(1 << i))
cout << i << " bit is 1" << endl;
else
cout << i << " bit is 0" << endl;
}
cout << "Your number is:" << num << endl;
}
int main() {
signed short num;
cout << "Please, enter your number:";
cin >> num;
print(num);
if (num&(1 << 15))
cout << "Your number is negative" << endl;
else
cout << "Your number is positive" << endl;
return 0;
}
答案 0 :(得分:0)
您可以结合使用匿名// include the needed localStorage manipulation methods
function store(key, data)
{
localStorage.setItem(key, JSON.stringify(data));
}
function retrieve(key)
{
var data = localStorage.getItem(key);
return data ? JSON.parse(data) : null;
}
function remove(key)
{
localStorage.removeItem(key);
}
// create and initialise the button
var btn = document.createElement("BUTTON");
btn.onclick = function() {myFunction()};
btn.style.height = "100%";
btn.style.width = "98%";
btn.style.border = "0px";
btn.innerHTML = "CLICK ME";
btn.id = "toggle";
document.getElementById("button").appendChild(btn);
var buttonState = retrieve('mybutton');
// make sure at this point element with id="notepad" exists on page
if ( null !== buttonState )
{
buttonState && document.getElementById('notepad').classList.add('hide');
}
function myFunction() {
var bt = document.getElementById('notepad');
bt.classList.toggle('hide');
store('mybutton', bt.classList.contains('hide'));
return false;
}
和位域来实现所需的行为:
union
请注意,如果我没记错的话:
union bit_access {
int16_t as_short;
struct {
uint8_t bit15 : 1;
// Continue until :
uint8_t bit0 : 1;
};
};
// You can then access individual bits using :
bit_access b = { -1234 };
std::cout << "bit 0 = " << (int) b.bit0 << std::endl;
的大小不能保证为16 unsigned short
不会被填充,您可能需要使用struct
之类的东西,它是特定于编译器的。